Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Js

Idiom #257 Traverse list backwards

Print each index i and value x from the list items, from the last down to the first.

[...items].reverse().forEach((item, index) => 
console.log(Math.abs(index -= items.length), item));

reverse a copy of the array items, then print each index and value.
Get the correct index by calculating the absolute value of the index - the length of the list
for(int i = items.Count - 1; i >= 0; i--)
{
    Console.WriteLine($"Index = {i}, Item = {items[i]}");
}

New implementation...
< >
programming-idioms.org