0

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`
    },
  ]
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
B. N. B
  • 41
  • 4
  • 1
    I tried to rewrite your question so it was readable and understandable. I also made you a snippet. Please add your attempt in a [mcve] – mplungjan Sep 09 '22 at 08:10
  • 1
    Also JS arrays start at 0 – mplungjan Sep 09 '22 at 08:10
  • @mplungjan please don't convert code that doesn't need to run to a snippet. A code block is fine. – Cerbrus Sep 09 '22 at 08:16
  • You were quite close, Note the (i+1) to show the 0-based counter of seasons: `for (let i = 0, seasons = myFavSeries.seasons.length; i < seasons; i++) { const season = myFavSeries.seasons[i]; for (let j = 1, episodes = season.episodes; j <= episodes; j++) { console.log("season", (i + 1), "episode", j) }` } – mplungjan Sep 09 '22 at 08:19
  • There are several ways to do it, but essentially they all involve a loop as you'll need to get every integer between 0 and the number of episodes. One other way to do it would be to use [Array.from()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from). I wrote an example of that here: https://jsfiddle.net/ya7ubcvt/ – icecub Sep 09 '22 at 08:21
  • 1
    @mplungjan I've [asked for opinions on Meta](https://meta.stackoverflow.com/questions/420279/should-we-help-noobs-write-snippets-if-the-code-doesnt-require-snippet-funct) on whether or not we should have snippets for cases like this. – Cerbrus Sep 09 '22 at 09:02
  • @Cerbrus they misunderstood your question so yeah - I still think newcomers should be taught to use snippets and then he could have added the attempt on the object instead of `for(i=1; i<=22; i++){ console.log(i); }` – mplungjan Sep 09 '22 at 09:18
  • Then please explain to them how they misunderstood it, @mplungjan – Cerbrus Sep 09 '22 at 09:20
  • @Cerbrus I did.. – mplungjan Sep 09 '22 at 09:31

1 Answers1

0
  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

Ben
  • 624
  • 6
  • 16
  • Thanks everyone my code is no working. I already postet a second question probably pretty easy to answer but I just can´t find a solution. – B. N. B Sep 09 '22 at 14:31