1

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'
 } 
   
3
  • 2
    It is return - not result Commented Jul 19, 2021 at 16:08
  • 1
    Also remove the semi-colon, and use booleans not strings. Commented Jul 19, 2021 at 16:08
  • change (result; 'false') to (return false;) the same with the "return true;". Commented Jul 19, 2021 at 16:09

4 Answers 4

2

A couple of typos: ; should be = and you should actually return a Boolean, not a String value:

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

console.log(isOldEnoughToVote(17)); // false
console.log(isOldEnoughToVote(18)); // true

Or, if you like more Arrow functions

const isOldEnoughToVote = (age) => age >= 18;

// The first => is an arrow function's "Fat Arrow"
// The second >= is an greater-or-equal operator

console.log(isOldEnoughToVote(17)); // false
console.log(isOldEnoughToVote(18)); // true

Regarding your code, there's also that response variable but you never assigned anything to it, instead you're trying to do something with that result; 'true'.

If you really need to return two strings "true" and "false" you can do it like:

function isOldEnoughToVote(age) {
  if (age < 18) {
    return "false";
  } else {
    return "true";
  }
} 

console.log(isOldEnoughToVote(17)); // "false"
console.log(isOldEnoughToVote(18)); // "true"

Or by using an Arrow Function and the Ternary operator ?:

const isOldEnoughToVote = (age) => age < 18 ? "false" : "true";

console.log(isOldEnoughToVote(17)); // "false"
console.log(isOldEnoughToVote(18)); // "true"

Or you could convert the Boolean to string using .toString():

const isOldEnoughToVote = (age) => (age >= 18).toString();

console.log(isOldEnoughToVote(17)); // "false"
console.log(isOldEnoughToVote(18)); // "true"

But I still think your task should be to return a Boolean, not a String :)

Sign up to request clarification or add additional context in comments.

1 Comment

It feels funny to see a the ternary truthy evaluation return the string false 😂 but in this example it also shows using another comparison operator for age and 18 and demonstrates the conditional well.
1

That error means the variable result within the function, is not defined.

You don't need to do that at all though, return the boolean directly.

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

In the above, you don't need the else since it would be the only other result. So you could do:

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

Or you can more simply return the result of the comparison.

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

2 Comments

Your second demo is incorrect. He needs to add else otherwise it will always return false
No, the return causes function to exit
0

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

1 Comment

Thank you! The return value makes a lot more sense thanks for the help.
0

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

console.log(isOldEnoughToVote(18));

console.log(isOldEnoughToVote(8));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.