FAQ: Code Challenges: JavaScript Fundamentals - truthyOrFalsy()

Can you please tell me what I’m doing wrong with my code:

const truthyOrFalsy = value => {
  if (value === true) {
    return true;
  } else {
    return false;
  }
}

console.log(truthyOrFalsy(0));

I got this error message:

If an argument that evaluates to truthy is passed in to the function, the function should return true

const truthyOrFalsy = x => Boolean(x);

This is one of those problems where a LOT of things work as a solution. But there is the one really really simple solution that solves all of the world’s problems…

const truthyOrFalsy = value => value ? true : false;

thinking about why this works is incredibly simple and also makes my brain hurt.

1 Like

My solution:

const truthyOrFalsy = (value) => {

if (value)

{return true}

else {return false}

}

Using logical operators…

const truthyOrFalsy = x => !! x;

Hello,
Why when you type NaN as the name of the value you get a syntax error??

You should see false

Can you show a screenshot of the error message, please? We’re not able to reproduce the error in sandbox.

I’m not seeing what I’m missing. the code is giving me an error!

const truthyOrFalsy = (username, Stranger) => {
if (username) {
truthyOrFalsy = username;
} else {
truthyOrFalsy = ‘Stranger’

}
};

// Uncomment the line below when you’re ready to try out your function
console.log(truthyOrFalsy(0)) // Should p

you can do it right even without the ternary operator

const truthyOrFalsy= num => {
if(num){
return true
}else {
return false
}
}

can you please explain or rewrite the code, I don’t fully understand it! thank you

The thing to do would be go back over all the previous lessons and look for tidbits that might have been missed the first time through.

1 Like