Loop + Array - how to make numbered list

Hello! I am supposed to “print the number and title for each value in titles. Make sure that the first in the list is 1 , not 0 .”

  1. The Philosopher’s Stone
  2. The Chamber of Secrets
  3. The Prisoner of Azkaban
    // and so on…

I know how to loop through the array, but unsure how to “number” the list. Thanks in advance!

titles = ['The Philosopher\'s Stone', 'The Chamber of Secrets', 'The Prisoner of Azkaban', 'The Goblet of Fire', 'The Order of the Phoenix', 'The Half-Blood Prince', 'The Deathly Hallows']; for (let i=0; i<=6; i++) { console.log(titles[i]); }

you could use i+1 inside the loop;
so that when the index, i, is 0, you get a 1 for i+1 ,
and when the index is 1, you get a 2 ,
and so on.

for (let i=0; i<=6; i++) {
  console.log( (i + 1) + ". " + titles[i] );
}

or:

for (let i=0; i<=6; i++) {
  console.log( `${i + 1}. ${titles[i}` );
}
2 Likes

Thank you so much for the reply! That did the trick. Appreciate the help!