Showing episodes as numbers

Viewed 36

We build a little program with some loops because this is what we are learning right now. Is there a way to write it so that the program sees the number and than gives them out i.e sees season 2 has 22 episodes and then gives out 1,2,3,4,5 and so on.

We now should give out the amount of episodes as the numbers i.e for season 2 it would be every number from 1 to 22.

I know I can look at how much episodes there are in a season and write

for(i=1; i<=22; i++){ console.log(i); }

I tried to look a solution on how to do this but I couldn't find one.

const myFavSeries = {
  title: `Scrubs`,
  director: `Bill Lawrence`,
  year: `2001 - 2010`,
  description: `In this series we follow JD as he starts his journey from a fresh medicine college alumni in to becoming a doctor. During this adventure he encounters a lot of ups and downs.`,
  seasons: [{
      episodes: 24,
      startYear: `02.10.2001`,
      endYear: `21.05.2002`
    },
    {
      episodes: 22,
      startYear: `26.09.2002`,
      endYear: `17.04.2003`
    },
  ]
}
1 Answers
  1. Access the list of episodes: myFavSeries.seasons
  2. Get the second season: myFavSeries.seasons[1]
  3. Get the number of episodes: myFavSeries.seasons[1].episodes

Then use your for loop, replacing 22 by the value found in step 3.

Note: If you need to do that for all seasons, you can change step 2 by a for loop from 0 to myFavSeries.seasons.length

Related