Skip to content

Instantly share code, notes, and snippets.

@commanderh
Created February 23, 2021 03:00
Show Gist options
  • Save commanderh/ffe1bd81ba997e875fce28ce86518b03 to your computer and use it in GitHub Desktop.
Save commanderh/ffe1bd81ba997e875fce28ce86518b03 to your computer and use it in GitHub Desktop.
/***********************************************************************
Write a recursive function `iceCreamShop(flavors, favorite)` that takes in an
array of ice cream flavors available at the ice cream shop, as well as the
user's favorite ice cream flavor. Recursively find out whether or not the shop
offers their favorite flavor.
Examples:
iceCreamShop(['vanilla', 'strawberry'], 'blue moon'); // false
iceCreamShop(['pistachio', 'green tea', 'chocolate', 'mint chip'], 'green tea'); // true
iceCreamShop(['cookies n cream', 'blue moon', 'superman', 'honey lavender', 'sea salt caramel'], 'pistachio'); // false
iceCreamShop(['moose tracks'], 'moose tracks'); // true
iceCreamShop([], 'honey lavender'); // false
***********************************************************************/
// your code here
let iceCreamShop = (flavors, favorite) => {
for(let i = 0; i < flavors.length; i++) {
const flavor = flavors[i];
if(flavor === favorite) {
return true;
}
}
return false;
};
console.log(iceCreamShop(['vanilla', 'strawberry'], 'blue moon')); // false
console.log(iceCreamShop(['pistachio', 'green tea', 'chocolate', 'mint chip'], 'green tea')); // true
console.log(iceCreamShop(['cookies n cream', 'blue moon', 'superman', 'honey lavender', 'sea salt caramel'], 'pistachio')); // false
console.log(iceCreamShop(['moose tracks'], 'moose tracks')); // true
console.log(iceCreamShop([], 'honey lavender')); // false
/**************DO NOT MODIFY ANYTHING UNDER THIS LINE*****************/
try {
module.exports = iceCreamShop;
} catch (e) {
module.exports = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment