Loops Practice Question: Harry Potter Titles

Hi all,

I’m having trouble with a Loops practice question.

Loop Practice

The results should be:

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...

The array is provided. I cannot figure out how to make this list start at 1 without omitting the first title (The Philosopher’s Stone).

This is what I have:

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 (i = 0; i < titles.length; i++) {
  console.log(i + '. ' + titles[i]);
}

Output:

0. The Philosopher's Stone
1. The Chamber of Secrets
2. The Prisoner of Azkaban
3. The Goblet of Fire
4. The Order of the Phoenix
5. The Half-Blood Prince
6. The Deathly Hallows

Any advice would be appreciated!

You’re setting i=0, so that’s why it starts at 0. If you change that though there’s one more element you need to change… Try it out :slight_smile:

1 Like

Thank you! I got it:

for (i = 1; i <= titles.length; i++) {
  console.log(i + '. ' + titles[i-1]);
}

Output:

1. The Philosopher's Stone
2. The Chamber of Secrets
3. The Prisoner of Azkaban
4. The Goblet of Fire
5. The Order of the Phoenix
6. The Half-Blood Prince
7. The Deathly Hallows
1 Like