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);
}
-
1Can you show us what you have tried?Thum Choon Tat– Thum Choon Tat2018年03月28日 06:18:08 +00:00Commented Mar 28, 2018 at 6:18
-
It;s in the code above. scroll down you will seeKunal– Kunal2018年03月28日 06:20:29 +00:00Commented Mar 28, 2018 at 6:20
-
Sorry I missed thatThum Choon Tat– Thum Choon Tat2018年03月28日 06:20:44 +00:00Commented Mar 28, 2018 at 6:20
3 Answers 3
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!');
Comments
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
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);