Code Not Working - Dog Years if /else


#1

Hi, I'm hoping someone can point out what I need to do to get this code to work. I want to use an if else statement, in the event that the response is 1 or 2.

var myAge = prompt("What's your age?");

var dogYears = if (myAge =< 2) {
var x = myAge * 10.5;
console.log(x);
}

else {
(myAge - 2) * 4 + x;
}

};


#2

This is invalid code. We cannot mix a variable declaration and if statement.

var dogYears;
if (myAge <= 2) {
    dogYears = myAge * 10.5;
} else {
    
}

I expect you will want to add 21 to age minus 2 times 4. Without looking at the lesson instructions this is just a guess. The above should help guide you along.

Aside

Note the comparison operator used above. The equal sign always comes last.

<=
>=
!=
!==

#3

Roy, thanks, but how can I do it? If the answer to the prompt is 1 or 2, I want to do one thing, and if it's 3 or greater, I want to do something else.


#4

In the lessons we learned about conditionals and control flow, and the number of ways that we can direct traffic based on satisfying certain conditions or states.

In your problem, the first state, as it were, is a string return value that looks like a number but that we cannot use in arithmetic. So we arbitrarily alter its state to a type of data that is suitable for doing math, a number, and in this case, an integer.

The next state we need to check is the actual value of the number. We need to be able to carry out different actions depending upon that number. What did you learn in the unit that will guide you through this process?