'Challenge: Short-Circuiting a Loop - Interview Practice

Question: Complete the function holidays that accepts an array of strings and iterates through the array. If the array contains the string "October", return "Happy Halloween". Otherwise, return the string "Have a great day!". Do not use a variable to store the result that you are returning.

So, here is what I have so far after many versions:

function holidays(arr) {
  let happyHalloween = "Happy Halloween";
  arr.forEach(item => {
    if (item === "October"){
      return happyHalloween;
    } 
  });
}

// Uncomment these to check your work!
const months = ["April", "May", "June", "October"];
const animals = ["Cats", "Dogs", "Pigs"];
console.log(holidays(months)); // should return: "Happy Halloween"
console.log(holidays(animals)); // should return: "Have a great day!"

Thank you guys in advance.

So, I know the second array, animals, would come out undefined as of now - I just normally tackle these problems step by step. I am just trying to get "Happy Halloween" first. But, From what I can tell, my code will iterate through the const "months" and eventually the if statement should work where (item === "October") and I think it should return the string? I'm a little new to Javascript - but does this maybe have to do with scope?

I only stored the string "Happy Halloween" in a const because when I tried return the string like this:

function holidays(arr) {
  arr.forEach(item => {
    if (item === "October"){
      return "Happy Halloween";
    } 
  });
}

// Uncomment these to check your work!
const months = ["April", "May", "June", "October"];
const animals = ["Cats", "Dogs", "Pigs"];
console.log(holidays(months)); // should return: "Happy Halloween"
console.log(holidays(animals)); // should return: "Have a great day!"

It returns undefined as well.

I'm assuming once I understand why it's returning undefined for the first array, months, I'll be able to work out the animals array and the other response by myself.



Solution 1:[1]

You could use array.prototype.includes() method to check if a given array includes a given value. For example:

function holidays(arr) {
arr.includes('October') ? 'Happy Halloween' : 'Have a great day!';
}

array.includes() returns true if the array being iterated includes the given search item. This is saying that if the given array includes 'October' the string 'Happy Halloween' will be returned, otherwise it will default to 'Have a great day!' if no match is found in the array.

Edited to add: the reason that you're getting undefined returned is because there is no "else" statement to fall back on if the array does not contain "October". The program knows to return something if there is a match found, but there is nothing that says what to do if there is no match found. This could be resolved by adding an else statement after the closing bracket of the first return. Basically:

function holidays(arr) {
  let happyHalloween = "Happy Halloween";
  arr.forEach(item => {
    if (item === "October"){
      return happyHalloween;
    } else return 'Have a great day!'
  });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1