HIgher Order Functions - undefined

Hi I was playing around with higher order functions after the lesson trying to get a better grasp. I wrote a function which works in principle but the output apart from the desired is throwing out undefined. I wonder if anyone could shed some light on what it is referring to?

const weatherCheck = temp => {
  if (typeof temp !== 'number') {
   console.log('Please enter correct temperature')
  } else if (temp > 20) {
    return 'yes'
  } else {
    return 'no'

const shouldIGoOnHolidays = (holFunction, currentTemp) => {
  let answer = holFunction(currentTemp);
  if (answer === 'yes') {
    console.log('The weather is nice go on ahead!')
    }   else if (answer === 'no') { console.log('You should probably stay at home')
  } else { console.log('Impossible to say')}
console.log(shouldIGoOnHolidays(weatherCheck, 21))

The weather is nice go on ahead!

Am I doing something wrong? I welcome any feedback :slight_smile:

Hey there,

if you don’t explicitly return anything from a function, it will return undefined by default.

You do return a string from your function weatherCheck, but not from the function shouldIGoOnHolidays. That is why the console with your function call logs undefined.

There are two ways to tackle that:

Either you just call your function without logging it. The called function logs it anyway. Or you replace the console.log within the function with a return and then log the function call as you currently do. The latter being the cleaner option, I think.

1 Like

thanks again! that also explains few other instances of the same issue

1 Like