2

I'm currently starting to learn JavaScript. However, can someone tell me what's wrong with the program below and how come only age 18 is being accepted? When age is <18 or >18, I am getting undefined result at the chrome console when I run this code from the console:

var age = prompt("What is your age?");
if (Number(age) < 18) {
  alert("Sorry, you are too young to drive this car. Powering off");
} else if (Number(age) > 18) {
  alert("Powering On. Enjoy the ride!");
} else if (Number(age) === 18) {
  alert("Congratulations on your first year of driving. Enjoy the ride!");
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
ronrob27
  • 21
  • 1
  • What do you mean by "when age is <18 or >18, i am getting undefined result at the chrome console"? Is it not printing the error messages? And you're using alert, not console.log. It won't put results in the console. It will popup an alert. – Carcigenicate Nov 07 '18 at 17:19
  • Where did you get undefined? I got it nowhere – Just code Nov 07 '18 at 17:19
  • 4
    it is working fine :) what is not working for you? – A l w a y s S u n n y Nov 07 '18 at 17:19
  • 2
    The code works perfectly. Not sure what you mean – Frontear Nov 07 '18 at 17:21
  • 3
    Don't worry about that....is just a `console` response...for example if you open the console and type `var test = 13` and press enter, it prints `undefined`...you can take a look at this Q&A for more details: https://stackoverflow.com/questions/14633968/chrome-firefox-console-log-always-appends-a-line-saying-undefined – Hackerman Nov 07 '18 at 17:23
  • 2
    Possible duplicate of [Chrome/Firefox console.log always appends a line saying undefined](https://stackoverflow.com/questions/14633968/chrome-firefox-console-log-always-appends-a-line-saying-undefined) – Hackerman Nov 07 '18 at 17:23
  • Thanks a lot for all of your quick replies. When i answer 18 to the prompt question "What is your age", there will be an alert of Congratulations on your first year of driving. Enjoy the ride!". However, when I answer < 18 and > 18, I am not getting any alert result but undefined in the chrome console...Thanks. – ronrob27 Nov 07 '18 at 17:32
  • I am expecting the alert statements if I answer < 18 or > 18 in the prompt command. – ronrob27 Nov 07 '18 at 17:33
  • The code as pasted works as expected – mplungjan Nov 07 '18 at 17:34
  • Ok. thanks everyone.. Maybe its just a problem on the console. – ronrob27 Nov 07 '18 at 17:45
  • Feel free to delete the question since it is not useful to anyone but you. – mplungjan Nov 08 '18 at 05:24

2 Answers2

0

The console environment in your browser is designed to take the very last statement expression in a program and evaluate it for a value and then show you that value. You get undefined if the statement does not return anything.

For example, if you type var x = 2 in your console, you will get undefined, but if you type x = 2, you will get 2.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
-1

If it's already a number you don't typically need to use Number() to convert it to a number.

And, if it's an normal integer there is no reason to believe that Number() should convert it to anything but the number.

The only possible issue is I can imagine is that "age" isn't the expected value when you run your code snippet.

Ruklav-Nomad
  • 323
  • 1
  • 3
  • 7
  • The code works and the question will be closed as "Not reproducible" - the problem is console always returning undefined when pasting stuff in – mplungjan Nov 07 '18 at 17:23