Prompt function


#1

Hi guys
I'm totally new to coding. Need your kind advice on below issue.
Here's my codes for this project:
var myAge=18;
var firstYears=2*10.5;
var laterYears=(myAge-2)*4;
var myAgeInDogYears=firstYears+laterYears;
console.log(myAgeInDogYears);
var myAge = prompt('What is your age?');

I put different number in the popup window but always get the same result "85"

I think the prompt function in the last line doesn't work... but I cannot figure out why...

thanks in advance!


#2

You always get the same result because you have

console.log(myAgeInDogYears);
// equals to
console.log(firstYears+laterYears);
// where firstYears = 2*10.5 => 21
// where laterYears = (myAge-2)*4 => (18 - 2) * 4 = 16 *4 = 64
// so
console.log(firstYears+laterYears) => console.log(21 + 64);

I thisnk should be:

var myAge = prompt('What is your age?');
var firstYears=2*10.5;
var laterYears=(myAge-2)*4;
var myAgeInDogYears=firstYears+laterYears;
console.log(myAgeInDogYears);

Now, every time when you call prompt() and put a number you will get another result.