8. all on your own, SyntaxError: Unexpected token {


#1

why is this happening?

var placeholder
var answer = prompt("What is your favourite single digit number","Write a number from 0 to 9")
if (isNaN(answer) = false {
var placeholder = true
} else {
    var placeholder = false };

#2

Okay, so we've just learned some basic concepts and been given a chance to replicate and emulate what we've just learned.. But the above seems to have wondered away from the group and got lost in the meadow.

Suggest, go back, and review the unit lessons. At this point we need to know what you have learned, not what you can do. Replicate what you are shown in the lessons and soon you will emulate the concepts they teach. Then you have something to build upon.

Please permit me to break it down for you. Thank you. As follows:

var placeholder

This appears three times in the code. Once would suffice. and while we're declaring variables may as well toss in 'answer` for free.

var placeholder, answer;

As we've learned in JavaScript we don't have to give variables a value (and as a result no type assignment) when we declare then. We've only reserved these data points in memory. What we assign will come later. What's more, we can change the type on the fly just be assigning new data.

Bottom line, only use var once, and never again on a variable.

The next issue that arises is differentiating between assignment and comparison.

will raise an exception. The reason is simple, we do not assign a value to the a function call return. Compare, maybe, but never assign.

And since isNaN() is a boolean expression, we don't need to compare it to anything. Just poll the expression.

if (isNaN(answer)) {
    placeholder = false;
}
else {
    placeholder = true;
}

The above reverses the question to become. 'is N a number?'


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.