0

Write a function that takes Season number and episode number as input and gives all the information about that particular episode as output

After taking inputs from the user as season number and episode number, it doesn't give output as the information about that particular episode

let BigBang =  { 
    "_embedded": {
        "episodes": [
          {
            "id": 2913,
            "name": "Pilot",
            "season": 1,
            "number": 1,
            "airdate": "2007-09-24",
            "airtime": "20:30",
            "airstamp": "2007-09-25T00:30:00+00:00",
            "runtime": 30,


            "_links": {
              "self": {
                "href": "http:\/\/api.tvmaze.com\/episodes\/2913"
              }
            }
          },
          {
            "id": 2914,
            "name": "The Big Bran Hypothesis",
            "season": 1,
            "number": 2,
            "airdate": "2007-10-01",
            "airtime": "20:30",
            "airstamp": "2007-10-02T00:30:00+00:00",
            "runtime": 30,
            "image": {
              "medium": "http:\/\/static.tvmaze.com\/uploads\/images\/medium_landscape\/4\/12369.jpg",
              "original": "http:\/\/static.tvmaze.com\/uploads\/images\/original_untouched\/4\/12369.jpg"
            },
       }



    let season = prompt('Enter Season number');                               
    let number = prompt('Enter Episode number');                            
    let AllInfo = (season,number) => {                                   
        for(let current in BigBang._embedded.episodes) {                    
            if(BigBang._embedded.episodes[current].season === season) {                                              
                if(BigBang._embedded.episodes[current].number === number) { 
                let Detail = BigBang._embedded.episodes[current];
                    alert(Detail);
                }
            }
        }                            
    AllInfo(season,number);                         
    }
3
  • 1
    Can you show us what you have tried? Commented Mar 28, 2018 at 6:18
  • It;s in the code above. scroll down you will see Commented Mar 28, 2018 at 6:20
  • Sorry I missed that Commented Mar 28, 2018 at 6:20

3 Answers 3

1

Try using .find instead, it'll make the code a lot cleaner:

    let BigBang = {
      "_embedded": {
        "episodes": [{
            "id": 2913,
            "name": "Pilot",
            "season": 1,
            "number": 1,
            "airdate": "2007-09-24",
            "airtime": "20:30",
            "airstamp": "2007-09-25T00:30:00+00:00",
            "runtime": 30,
            "_links": {
              "self": {
                "href": "http:\/\/api.tvmaze.com\/episodes\/2913"
              }
            }
          },
          {
            "id": 2914,
            "name": "The Big Bran Hypothesis",
            "season": 1,
            "number": 2,
            "airdate": "2007-10-01",
            "airtime": "20:30",
            "airstamp": "2007-10-02T00:30:00+00:00",
            "runtime": 30,
            "image": {
              "medium": "http:\/\/static.tvmaze.com\/uploads\/images\/medium_landscape\/4\/12369.jpg",
              "original": "http:\/\/static.tvmaze.com\/uploads\/images\/original_untouched\/4\/12369.jpg"
            },
          }
        ]
      }
    }
    //const inputSeason = prompt('Enter Season number');
    const inputSeason = 1;
    //const inputNumber = prompt('Enter Episode number');
    const inputNumber = 2;

    const foundEpisode = BigBang._embedded.episodes.find(({ season, number}) => {
      return season === inputSeason && number === inputNumber;
    });
    if (foundEpisode) console.log(foundEpisode);
    else console.log('No matching season/number found!');

Sign up to request clarification or add additional context in comments.

Comments

0

You could do that easier with the find() method on your array.

let episode = BigBang._embedded.episodes.find((e) => {
    return e.season === season && e.number === number;
});
if (episode) {
    alert(episode.name);
}

Comments

0

I have debugged your code and saw that you call AllInfo function within AllInfo .So there is recursive call happen in your code. So remove calling of AllInfo from AllInfo function, your issue will be fixed. Try the following code.

let BigBang =  { 
"_embedded": {
    "episodes": [
      {
        "id": 2913,
        "name": "Pilot",
        "season": 1,
        "number": 1,
        "airdate": "2007-09-24",
        "airtime": "20:30",
        "airstamp": "2007-09-25T00:30:00+00:00",
        "runtime": 30,


        "_links": {
          "self": {
            "href": "http:\/\/api.tvmaze.com\/episodes\/2913"
          }
        }
      },
      {
        "id": 2914,
        "name": "The Big Bran Hypothesis",
        "season": 1,
        "number": 2,
        "airdate": "2007-10-01",
        "airtime": "20:30",
        "airstamp": "2007-10-02T00:30:00+00:00",
        "runtime": 30,
        "image": {
          "medium": "http:\/\/static.tvmaze.com\/uploads\/images\/medium_landscape\/4\/12369.jpg",
          "original": "http:\/\/static.tvmaze.com\/uploads\/images\/original_untouched\/4\/12369.jpg"
        },
   }]}};



let season = 1;                               
let number = 2;                            
let AllInfo = (season,number) => {                              
    for(let current in BigBang._embedded.episodes) {                    
        if(BigBang._embedded.episodes[current].season === season) {                                              
            if(BigBang._embedded.episodes[current].number === number) { 
            let Detail = BigBang._embedded.episodes[current];
                alert(JSON.stringify(Detail,null,4));
            }
        }
    }                                                     
}
AllInfo(season,number);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.