FAQ: Code Challenges: JavaScript Fundamentals - canIVote()

Community%20FAQs%20on%20Codecademy%20Exercises

This community-built FAQ covers the “canIVote()” exercise from the lesson “Code Challenges: JavaScript Fundamentals”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise canIVote()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello,
I need help with canIVote() exercise.
Why this code is wrong?

const canIVote = (age) => {
(age >= 18) ? return true : return false;
}

console.log(canIVote(19))

const canIVote = (age) => {
(age >= 18) ? return true : return false;
}

console.log(canIVote(19))

Below is the code I wrote for the canIVote(). When I test the code it holds up but it is telling me I am wrong… confused as to why…
//My Code for canIVote()
const age = 16;
const canIVote = () => age >= 18 ? true : false;
console.log(canIVote());

I’m having the same problem as the other two commenters, really hoping someone can help.

My code is as followed:

const canIVote = age => {
age >= 18 ? return true : return false;
}

I get a SyntaxError for unexpected token return. When I remove return, I don’t get the error code but then the only output of the function when called is “undefined”.

Can anyone help? I’ve gone through my notes and can’t figure out what the problem. If I write it as a regular if/else statement it does work fine, but it’s just the refactored code that I’m having issues with.

I believe I have solved the problem with the ‘undefined’ output. Simply remove the curly brackets from your single line block! I hope this helps.

const canIVote = (age) => age >= 18 ? true : false;

A function body composed of a single-line block does not need curly braces. Without the curly braces, whatever that line evaluates will be automatically returned. The contents of the block should immediately follow the arrow => and the return keyword can be removed. This is referred to as implicit return .

7 Likes

This is what I used, and it worked:

function canIVote(age) {
if (age >= 18) {
return true;
}
else {
return false;
}
}

I really like your 1 line solution.

const canIVote = (age) => age >= 18 ? true:false; I still need to practice more with arrow syntax

Consider,

What will this expression yield? Answer: A boolean.

Relational expressions are boolean so we never need to write boolean literals. Just return the expression. If the relation is truthy, then true will be returned, else false.

const canIVote = age => age >= 18;
2 Likes

Does this assignment just want a condensed function?

I’m a little confused. I thought for sure this would work. Please help!

Compound comparison operators (inequalities) always have the = written last.

>=

<=

!=
2 Likes

Thank you so much! That was driving me crazy. :relieved:

1 Like

That makes so much sense too. As to not interfere with the fat arrow.

I too am having some issues with this piece of code… I feel like it should run, but I keep get an error stating there is an unexpected token in the form of the else. Any suggestions on what I’ve done wrong and why?

const canIVote = age => {
  if (age >= '18') //missing { //Also, assuming age is a number, 18 should not be in qoutes.
    return True;
  } else {
     return False;
  }
};

By having the 18 in quotes, you are comparing a number to a string.

Hey - thanks for responding - I wondered that but still get the same error messages when I run it?

Oh - never mind - I missed your curly bracket comment - and yes that made all the difference! Thanks heaps.

True and False are not valid primitives. They should be true and false.

Yes - thanks for that!

1 Like

Just takes practice to get used to all the necessary curly braces. Before long it will be second nature. Good luck! :grinning:

Cheers! I am finding JavaScript a bigger brain stretch than html and css!

1 Like