Skip to content
Advertisement

Function return boolean statement

Task Instructions

Your task in this activity is to create a function that checks if a person is old enough to vote by checking their age. This function is called isOldEnoughToVote(age) and has the following specifications: It takes an argument called age representing the age of the person. It checks if the age is greater than or equal to 18. If returns true or false based on that comparison.

This is what I’ve written so far but It says result is not defined and I’m wondering why.

let response;
var age = 18
// Add your code here
function isOldEnoughToVote(age) {
 if (age >= 18){
   result; 'true'
 }else{
   result; 'false'
 } 
   

Advertisement

Answer

Your code example is using result; 'true' (for example) to indicate a true result. This doesn’t do anything – in fact it’s not correct at all.

Instead it should use return true:

let response;

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

console.log(isOldEnoughToVote(10));
console.log(isOldEnoughToVote(18));
console.log(isOldEnoughToVote(50));

However, this could be simplified even further by just returning the result of age >= 18:

function isOldEnoughToVote(age) {
  return age >= 18;
}

console.log(isOldEnoughToVote(10));
console.log(isOldEnoughToVote(18));
console.log(isOldEnoughToVote(50));
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement