FAQ: Code Challenges: JavaScript Fundamentals - canIVote()


#1

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!


#2

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))


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

console.log(canIVote(19))

#4

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());


#5

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.


#6

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 .