Could anyone help me with Lesson 5.)


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-ZA2rb/0/5

I'm getting: "SyntaxError: Unexpected Token ."

I don't see what I'm entering incorrectly, can anyone lend a second pair of eyes?


var user = prompt("You've just moved to a new city, do you plan to live alone, look for a roommate or play it by ear?").toLowerCase();

switch(user) {
    case "plan to live alone":
        var social = prompt("Are you ok with not being too social? (yes or no)").toLowerCase();
        var money = prompt("Are you making a lot of money? (yes or no)").toLowerCase();
    if(social || money === "yes") {
        var console.log("You'll probably be ok then, just make sure to work hard - it's expensive")
    } else {
        console.log("You'll probably run out of money or crumble from the lonliness then. I'd reconsider if I were you")
    }
    
        break;
    case "look for a roommate":
        var friendly = prompt("Are you a pretty friendly person?").toLowerCase();
        var clean = prompt("Are you a neat freak?").toLowerCase();
    if(friendly && clean === "yes") {
        console.log("You're gonna find someone really quick if you're clean and friendly!")
    } else {
        console.log("Develop your empathy AND clean up your act, then I think you'd be a proper fit for a roommate pairing")
    }
        break;
    case "play it by ear":
        alert("You won't make it - reconsider.");
        var findSomething = prompt("Say you do find something, it won't be ideal and you'll be locked into that for at least a year! Will you be ok with that? (yes or no)").toLowerCase();
        var dontFindSomething = prompt("If you don't find something, will you live in the street?").toLowerCase();
        if(findSomething && dontFindSomething === "yes") {
            console.log("Then go for it kid, best of luck to you")
        } else {
            console.log("Then go back to school and prepare more. It isn't worth the struggle.")
        }
        break;
    default:
        console.log("Choose 'plan to live alone', 'look for a roommate' or 'play it by ear' to continue.");
}

#2

Try removing the var keyword from this line. That's where the unexpected token . is found. The interpreter thinks you are declaring a variable, console. (never do that, btw).


#3

Thanks, this solved it!


#4

Something you should know about prompt()...

It has two possible data types in the return value. A string when the Enter/Return key is pressed, or Okay is clicked, and null when we click Cancel or press Esc. The first is not a problem, but the second will throw a fatal error if we try to run a string method on it.

null has no property, 'toLowerCase'.

or something of this nature will pop up and terminate the program. We need to allow the user to Cancel. This means more logic in our code. In other words, never run a method on prompt(). Test the return value for stringiness before calling the method on it.

s = prompt(' ... ');
t = s !== null ? s.toLowerCase() : s;
if (t === null) break;

as one of many possible examples. Modularization and loop constructs aid in the process of allowing a smooth exit from the program, immediate or gradual, as applies.