2nd if statement


#1

When submitting my code I keep receiving a syntax error, not sure where I'm going wrong, thanks for the help?

//variable to check if we're still slaying
var slaying = true;

//variable to checkk if we hit the dragon
var youHit = Math.floor(Math.random() * 2);

// keep track of how much damage has been dealt to dragon this round
var damageThisRound = Math.floor (Math.random() * 5 + 1);

var totalDamage = 0;

// while loop that only executes when slaying = true
// set slaying = false in body of loop
while (slaying) {
   // commented out  slaying = false;
    
    // if/else statment that checks value of youHit
    if (youHit = true) { // open if
        console.log("You have hit the dragon!");
    
        // use shortcut operator 
        totalDamage += damageThisRound;
        
        // second if statement
        if (totalDamage >= 4) { // open 2nd if
            console.log("Player wins!");
            slaying = false;
        } // close 2nd if
        
        else { //open else 
            totalDamage <= 4;
            youHit = Math.floor (Math.random() * 2);
        } // close else
        
    else { // open 2nd else
        console.log("You have missed the dragon!");
    } // close 2nd else
    
    // Change the value of slayingn to false in while loop
    slaying = false;
}

#2

Yes just like me I have a lot of suntax problem ! At the end of your code the else statment ends by }} as it is shown in the hint .
I see that you made the same mistake as I did : the slaying=false statment must be included in the 2nd else { } statment . I put it outside as well .


#4

@cfarri4436,

while (slaying) {
   // commented out  slaying = false;
    
    // if/else statment that checks value of youHit
    if (youHit == true) { // open 1st if  <<<<<<<<<<<
        // with a falsy/truthy you cannot use strict comparison ===
        // we would use =youHit= directly like =slaying=
        console.log("You have hit the dragon!");
    
        // use shortcut operator 
        totalDamage += damageThisRound;

        // second if statement
        if (totalDamage >= 4) { // open 2nd if
            console.log("Player wins!");
            slaying = false;
        } // close 2nd if
        
        else { //open else 
            // totalDamage <= 4;  <<<<<<<<useless
            youHit = Math.floor(Math.random() * 2);
        } // close else
    } //closing the 1st if <<<<<<<<<<<<<<<< FORGOT THIS !!!
    else { // open 2nd else
        console.log("You have missed the dragon!");
        // and you would be dead
        // set =slaying= to =false=
    } // close 2nd else
    
    // Change the value of slayingn to false in while loop
    // this is set in development phase
    // preventing an =infinite loop=
    slaying = false;
}

Read comparison type-converting-comparison == OR strict-comparison ===
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
"1" == 1 will give a Boolean true, as the string "1" is first converted to a number Value
"1" === 1 will give a Boolean false as a string is of a different data-type as a number

Read falsy OR truthy
https://developer.mozilla.org/en-US/docs/Glossary/Falsy
https://developer.mozilla.org/en-US/docs/Glossary/Truthy

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html <---JavaScript code-convention
http://javascript.crockford.com/survey.html


#5

Thanks for the help on with this solution and your detail explanation along with references