If we want to display all the elements of the days
array in reverse order (starting from the last element) then we can do this using the statement:
-
for(let i = days.length - 1; i > 0; i--) console.log(days[i]);
-
for(let i = days.length; i > 0; i--) console.log(days[i]);
-
for(let i = days.length; i > 0; i--) console.log(i);
-
for(let i = days.length - 1; i >= 0; i--) console.log(days[i]);
Explanation & Hint:
A for loop is used to iterate over the elements of the days array in reverse order. The loop starts from the index of the last element (days.length – 1) and decrements the index (i) by 1 in each iteration until it reaches 0. Inside the loop, console.log(days[i]) is used to display each element of the array. By iterating in reverse order, you can effectively display all the elements of the days array starting from the last element and going towards the first element. |