diff --git a/Food-Recipe/index.js b/Food-Recipe/index.js index 5cd014a..7178d94 100644 --- a/Food-Recipe/index.js +++ b/Food-Recipe/index.js @@ -136,73 +136,69 @@ async function fetchResponses(recipeName) { } const data = await response.json(); - console.log('Number of results : ', data.results.length); - + if (Array.isArray(data.results) && data.results.length === 0) { - return false; - } else { - const thumbnail = data.results[0].thumbnail_url; - const video_url = data.results[0].original_video_url; - const description = data.results[0].description; - - const displayName = data.results[0]?.name; - - const countryTag = () => { - let result = ""; - data.results[0]?.tags.filter((entry) => { - if (entry.root_tag_type === "cuisine" && entry.display_name !== "Cuisine") - result += `${entry.display_name} `; - }); - return result.length === 0 ? "N/A" : result; - }; - - const rating = Math.ceil(data.results[0].user_ratings.score * 5); - const yields = data.results[0].yields; - const cookTime = data.results[0].total_time_tier?.display_tier ?? "N/A"; - - const ingredientsTag = data.results[0]?.sections[0].components; - console.log(ingredientsTag) - const instructionsTag = data.results[0]?.instructions; - - - const difficultyTag = () => { - const master = data.results[0]?.tags; - const filteredDifficultyTags = master.filter((entry) => { - return ( - entry.root_tag_type === "difficulty" && - (entry.display_name === "Easy" || - entry.display_name === "Medium" || - entry.display_name === "Difficult" || - entry.display_name === "Hard") - ); - }); - const result = filteredDifficultyTags.map((entry) => entry.display_name); - return result; - }; - - const nutrition = () => { - const obj = data.results[0]?.nutrition; - let result = ""; - if (obj) { - for (const key in obj) { - if (obj.hasOwnProperty(key) && key !== "updated_at") { - result += `${capitalize_firstLetter(key)}: ${obj[key]}, `; - } + console.log('No results found'); + } + + const thumbnail = data.results[0].thumbnail_url; + const video_url = data.results[0].original_video_url; + const description = data.results[0].description; + + const displayName = data.results[0]?.name; + + const countryTag = () => { + let result = ""; + data.results[0]?.tags.filter((entry) => { + if (entry.root_tag_type === "cuisine" && entry.display_name !== "Cuisine") + result += `${entry.display_name} `; + }); + return result.length === 0 ? "N/A" : result; + }; + + const rating = Math.ceil(data.results[0].user_ratings.score * 5); + const yields = data.results[0].yields; + const cookTime = data.results[0].total_time_tier?.display_tier ?? "N/A"; + const ingredientsTag = data.results[0]?.sections[0].components; + + const instructionsTag = data.results[0]?.instructions; + + const difficultyTag = () => { + const master = data.results[0]?.tags; + const filteredDifficultyTags = master.filter((entry) => { + return ( + entry.root_tag_type === "difficulty" && + (entry.display_name === "Easy" || + entry.display_name === "Medium" || + entry.display_name === "Difficult" || + entry.display_name === "Hard") + ); + }); + const result = filteredDifficultyTags.map((entry) => entry.display_name); + return result; + }; + + const nutrition = () => { + const obj = data.results[0]?.nutrition; + let result = ""; + if (obj) { + for (const key in obj) { + if (obj.hasOwnProperty(key) && key !== "updated_at") { + result += `${capitalize_firstLetter(key)}: ${obj[key]}, `; } - result = result.slice(0, -2).split(',').map(item => `