Code your own adventure 2: Unexpected Token If SyntaxError! But, why?


#1

Here's the code

var user = prompt ("As you look upon the golden chalice 'Altar of Akseni' you TAKE it, LEAVE it, or SET A TRAP around it?").toUpperCase();

if (user === "SET A TRAP") {
    console.log ("You decide it is best to" + " " + user + " " + "around the golden chalice!") } else { console.log (" You decide to" + " " + user + " " + "the golden chalice!") };

switch (user) {
    case "TAKE":
        confirm ("That chalice is looking a little... fiery...")
        var coveredHands = prompt ("Are your hands are covered?", "YES or NO").toUpperCase();
        var tempSense = prompt ("Either way, are you good with temperature defferences?" , "YES or NO").toUpperCase();
        if ((tempSense||coveredHands) === "YES") {
            //Why doesnt what I have starting here
            console.log ("Good thing" + " " +
            if (tempSense === "YES") {
                //The above if is the unexpected if causing the 
                //SyntaxError
                console.log ("you can handle temperatures well because this golden chalice is blessed under the power of the sun, forged in the flames of the Everburn and is still brimming with a fair bit of extra heat, but you should be able to manage.") }
                else {console.log (" your hands are covered because this golden chalice is blessed under the power of the sun, forged in the flames of the Everburn and is still brimming with a fair bit of extra heat, but you should be able to manage.")  }}
            else { 
                console.log ("You decide it's probably for the best that you don't pick it up due to the fact that you can already feel the excess heat it's putting off.") }
        //And ending here work? It says SyntaxError: Unexpected token if, which I understand, but WHY cant I do this, or is it that this just isn't the format to do it in? (Also, I got this to work before I did this, I just wanted to try this to see if I could get it to work because the end result just seems a little strange sounding.)
        break;
    case "LEAVE":
        confirm ("You decide you no longer want any business with this chalice, but as you nervously and spradically turn to leave you bump a hidden stone swith on a pillar, and the temple rattles and rolls as the it starts to fall apart!")
        var swiftFeet = prompt ("Can you run fast, and we're talking you're going to be ground into a pile of muck, fast?" , "YES or NO").toUpperCase();
        var emptyHanded = prompt ("Would it be wise to leave what you can behind to lighten the load, and move faster? Maybe even removing and ditching your armour as you go about it?" , "YES or NO").toUpperCase();
        if ((swiftFeet||emptyHanded) === "YES") {
            console.log ("With all the might in your legs and all the space in your lungs, you flee for this cabal of death to the white wheaten fields from which you came!") } 
            else {
                console.log ("You make it always entirely through the exit as one of your legs become crushed beneath a boulder, cleaving it off at your knee.  With all the resolve you can muster, however, you believe you can make it to the nearby city of Tri'atta and in turn survive.") }
        break;
    case "SET A TRAP":
        confirm ("You know full well you arent the only one here, you can feel the presence of at least one other.  However the entity you sense is mysterious, and takes on the presence of, at times, many.  Whether it be one or all, you know you're not alone.")
        var checkSense = prompt ("Do you have the ability to avoid detection while you rig a sufficient trap?" , "YES or NO").toUpperCase();
        var intelligence = prompt ("Are you even intelligent enough to build a trap?" , "YES or NO").toUpperCase();
        if((checkSense&&intelligence) === "YES") {
            console.log ("Creeping through the shadows, footfalls lighter than a louse, you use your wits to spring a trap that should ensnare any creature, ethereal or otherwise, in a magical matrix long enough for you to then escape, chalice in hand.") }
            else {
                console.log ("As you begin to set the trap, you become vividly aware that you're neither sharp enough to pull off the complex runes, nor quite enough to have gone still unnoticed.  The Pi'e'ren finds you and attaches itself to you, sucking the life from you and leaving you a dry, withered shell on the floor...") }
        break;
    default:
        console.log ("What, no, that clearly wasn't an option, this is my game you'll follow my rules! MINE MINE MINE!")
        break;
}

Now, as you can see indicated by the notes, I got this to run before I made the edit in the portion between the notes. I'm trying to get an if statement to run in the middle of a console.log command, I was curious, and am going to go and try to change the console.log command to a return command, what exactly is happening and why it can't do this?
If it's simply because this just isn't the way it's done, but it can be done, as much as I'd love for you to explain it, you don't have to! Just let me know if it's something that I will learn about or can learn about later down the road!

Thanks a bunch and happy learning everyone!


6 d.r.y
#2

The line above with the console.log(.. isn't finished, has an unclosed parenthesis and missing a second argument to +-operator
If that's intended, then I'm not sure what you're trying to do with that?


#3

I tried, closing it with a parenthesis, I also tried it without the + stuff that is there. It looked like this,

console.log ("Good thing")

originally, and I was hoping that closing it and starting a new if/else statement before closing the entire operation with a } would cause it to then run the second if/else statement. I haven't tried running it as an if/else if/else because I just don't feel like that's the right operation to use here, but, is it?


#4

That makes the syntax error go away on my end.


#5

What I want it to do, however, is go along the lines of if you decide to take the chalice, I want it to then determine that you if you did indeed take it based off your answers which answer was yes, if not both answers, to put forward text that was more fitting to what you said yes to if you didn't say yes to both. I am realizing now however that THIS part might need to be an if/else if/else statement though, because 3 things could have occurrred: Yes / No, No / Yes, Yes/ Yes...


#6

Hm I'll go re-run it, it might have just needed me to hop off for a minute so it could have a breather.


#7

ITS WORKING. YES! Not as pretty as I want it, but, it's working and that's a small step in the right direction at least.


#8

You might want to rethink this:

(tempSense||coveredHands) === "YES"

What it won't do is compare each of those two variables to the string "YES".
It will apply || to the two variables, and compare the result of that to "YES"

So for example:

"NO" || "YES   --->   "NO"

Which is not equal to "YES"


#9

Yeah I'm just now noticing that as I run through the option....
But this part I don't understand how to fix... Hm.... Any tips? :smiley:


#10

Compare each one to "YES"


#11

So... like this?

if (coveredHands === "YES") {
//all that string data }
if (tempSense === "YES") {
//all that string data again}

Would that be what you're meaning?


#12

Yes. But you can combine that into a single expression

..if one is equal to yes, OR the other is equal to yes


#13

I thought that's what

 if ((tempSense||coveredHands) === "YES") {
            //Why doesnt what I have starting here
            console.log ("Good thing" )
            if (tempSense === "YES") {
                //The above if is the unexpected if causing the 
                //SyntaxError
                console.log ("you can handle temperatures well because this golden chalice is blessed under the power of the sun, forged in the flames of the Everburn and is still brimming with a fair bit of extra heat, but you should be able to manage.") }
                else {console.log (" your hands are covered because this golden chalice is blessed under the power of the sun, forged in the flames of the Everburn and is still brimming with a fair bit of extra heat, but you should be able to manage.")  }}
            else { 
                console.log ("You decide it's probably for the best that you don't pick it up due to the fact that you can already feel the excess heat it's putting off.") }
        //And ending here work? It says SyntaxError: Unexpected token if, which I understand, but WHY cant I do this, or is it that this just isn't the format to do it in? (Also, I got this to work before I did this, I just wanted to try this to see if I could get it to work because the end result just seems a little strange sounding.)
        break;

did though...


#14

I thought we established that it doesn't! Just like your parentheses show, the || is evaluated before the ===, and what's the result of "NO"||"YES"?


#15

According to the thing it said that
"Yes" || "No"
"No" || "Yes"
and
"Yes" || "Yes"
all evaluated to the same thing; Yes (or rather true, but in this case yes === true...)

So the sub comparisons in the string can't be === "YES" because it's already been evaluated?
So they have to be set to something else?


#16

"No" || "Yes" evaluates to "No", because "No" is a non-empty string, so it is truthy.

0 || 3 // 3
5 || 3 // 5
5 && false // false
5 && 0 // 0
"hi" && "there" // "there"
"hi" || "there" // "hi"
false || true // true

#17

So do I need to set preexisting global variables of "YES" = true and "NO" = false?


#18

Those are strings, you can't assign to them, they can't have values, they are values


#19

That's what I thought but I wasn't positive.
Did I mention this is my first time ever working with coding and this is all very very foreign to me.


#20

That's why I'm making this difficult for you. So that you can figure out why it needs to be a certain way.

So back to like post number 3 or something, you need to compare each string to "YES". What value would you get as a result of each such comparison, and how would you combine those values?