HackAttack101
HackAttack101

Reputation: 35

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'
 } 
   

Upvotes: 1

Views: 4730

Answers (4)

Roko C. Buljan
Roko C. Buljan

Reputation: 206669

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

Upvotes: 2

dacodr
dacodr

Reputation: 167

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

Upvotes: 1

async await
async await

Reputation: 2405

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

Upvotes: 0

Martin
Martin

Reputation: 16453

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

Upvotes: 0

Related Questions