Search⌘ K
AI Features

Solution Review: Let's Vote

Understand how to implement a JavaScript function that evaluates voting eligibility by age. Explore input validation, strict mode usage, and conditional logic to return appropriate messages for different age values and data types.

We'll cover the following...

Solution

Run the code below to see the output.

Javascript (babel-node)
'use strict';
const canVote = function(age) {
if(age === 18) {
return 'yay, start voting';
}
if(age > 17) {
return "please vote";
}
return "no, can't vote";
};
console.log(canVote(12));
console.log(canVote("12"));
console.log(canVote(17));
console.log(canVote('@18'));
console.log(canVote(18));
console.log(canVote(28));

Explanation

First, we need to deduce some points from the sample input and output to implement this function. The function takes age as input and returns a string depending upon the value of age.

Deductions

...