Carlos Linares
Carlos Linares

Reputation: 1

JavaScript function not doing what is supposes to

The function is not throwing the expected output when called. the return statement is not working.

I check line by line for a code mistake.

function checkDriverAge(age) {
  if(Number(age) < 18) {
    var x = "Sorry you are to yound to drive this car. Powering off";
    return x;
  } else if(Number(age) === 18) {
    var y = "Congratulations of your first year of driving. Enjoy de ride";
    return y;
  } else if(Number(age) > 18) {
    var z = "Powering On. Enjoy the ride!";
    return z;
  }
}

checkDriverAge(prompt("Input an age"));

The expected result is to output the string specified inside the respective "if" relative to the input age.

Upvotes: 0

Views: 110

Answers (2)

larz
larz

Reputation: 5766

Looks like everything was working right, but you were just returning your message, you were never doing anything with it. Adding alert() or console.log() will display your message.

I also tidied up your function a little bit. No reason to assign three different variables.

function checkDriverAge(age) {
  let message;
  if (Number(age) < 18) {
    message = "Sorry you are to yound to drive this car. Powering off";
  } else if (Number(age) === 18) {
    message = "Congratulations of your first year of driving. Enjoy de ride";
  } else if (Number(age) > 18) {
    message = "Powering On. Enjoy the ride!";
  }

  return message;
}

alert(checkDriverAge(prompt("Input an age")));

Upvotes: 0

Kyle P
Kyle P

Reputation: 54

So, the way the code is written, the value coming out of checkDriverAge(prompt("Input an age")); isn't being assigned to anything.

Try assigning it to a variable, logging it out, or alerting the value.

console.log(checkDriverAge(prompt("Input an age")));
var x = checkDriverAge(prompt("Input an age"));
alert(checkDriverAge(prompt("Input an age")));

Upvotes: 1

Related Questions