5. The second 'if' statement


#1

Hello, I am confused about where to put semicolons and curley brackets. Plus my code is broken and need help with that as well.

var slaying = true
var youHit = Math.floor(Math.random() * 2)
var damageThisRound = Math.floor(Math.random() * 5 + 1)
var totalDamage = totalDamage + damageThisRound

while (slaying) {
if (youHit = true) {
console.log("You hit!");
totalDamage += damageThisRound;
if (totalDamage >= 4)
console.log("You slew the dragon!");
slaying = false;
if (totalDamage > 4) youHit = Math.floor(Math.random() * 2);
else(youHit = false)
console.log("the dragon defeated you.");
slaying = false;}
}


#2

can you send a link? the error might help too but I think I already see some syntax errors.


#3

Sure:
https://www.codecademy.com/courses/javascript-beginner-en-mrTNH-6VIZ9/0/5?curriculum_id=506324b3a7dffd00020bf661#


#4

you don't need the = true because it already checks if its true. it isn't a big deal but since you only have one = instead of == or === it causes problems. one = assigns a value, == checks value but ignores data type, and === checks data type and value.


#5

this can just be an else statement. you will have two in a row.
like this
else{
youHit = Math.floor(Math.random() * 2);
}else{
console.log("you missed!");
slaying = false;
}
}


#6

OK, I understand that though there are still syntax errors. When I compare my code to the 'hint' I have more curly brackets but I am still confused.


#7

OK, I "understand you don't need the = true because it already checks if its true."

Bu I am still stuck on the c-brackets:

var slaying = true
var youHit = Math.floor(Math.random() * 2)
var damageThisRound = Math.floor(Math.random() * 5 + 1)
var totalDamage = totalDamage + damageThisRound

while (slaying) {
if (youHit) {

	console.log("You hit!");
	totalDamage += damageThisRound;

	if (totalDamage >= 4) 
	console.log("You slew the dragon!")
	slaying = false;} 

	else
	{youHit = Math.floor(Math.random() * 2);} 

	else
	{(youHit = false)
	console.log("you missed.");}

slaying = false;}

}


#8

so it seems like you are confused about the syntax.
an if/else if/else statement should look like this

if(){

}else if(){

}else{

}
notice that the else part doesn't have parenthesis which means that it doesn't take parameters. if you are checking if something happens then you should be using an if or else if, because the else is for when none of the options are met. its kind of like a backup answer, like telling the player that they used an option that you cant except so they have to change their answer.

a While loop should look like this

while (){

}
while loops are pretty tricky because if you mess up you can crash your browser and lose all of your code. make sure that when you make a while loop that you make the parameter false before you end the loop so you can avoid that problem.
so it could look like this
var boolean = true;
while (boolean) {
boolean = false;
};

this might help with looking for errors in your brackets.


#9

so after you type the parenthesis you have to open the curly braces"{" and once you open that if the condition is met, all of the code inside of your curly braces will run. else if is basically like or if this happens then do this instead. but you close the curly braces at the end of the if statement and open them after the else if part like this

if(){

}else if(){ <-------notice the close before you type else if

}


#10

I'm still confused becauseI don't have an 'else if' in my code, do I?
while (slaying) {
if (youHit) {

	console.log("You hit!");
	totalDamage += damageThisRound;

	if (totalDamage >= 4) {
	console.log("You slew the dragon!")
	slaying = false;} 

	} else{
	    youHit = Math.floor(Math.random() * 2);
	    } 

	else
	console.log("you missed.");}

slaying = false;
}

}


#11

you don't I was just explaining the syntax. you have else statements that have parameters and that doesn't work. I'm currently trying to clean everything up on your code so sorry if I take a while


#12

so now your code looks almost exactly like mine and it still says there is a syntax error.

var slaying = true
var youHit = Math.floor(Math.random() * 2)
var damageThisRound = Math.floor(Math.random() * 5 + 1)
var totalDamage = 0
while (slaying) {
if (youHit) {
console.log("You hit!");
totalDamage += damageThisRound;
if (totalDamage >= 4){
console.log("You slew the dragon!");
slaying = false;
}else{
youHit = Math.floor(Math.random() * 2);
}else{

console.log("the dragon defeated you.");
slaying = false;
}
}


#13

NP.

With this exercise, is there an if inside an if? For ex,

if (youHit) {
    
	console.log("You hit!");
	totalDamage += damageThisRound; <---- accd. to the hint, there is not a curley bracket after this. Bit I thougtht the curly bracket had to close before the next if or else statement like this:

if () {}


#14

This is the hint:

while (slaying) {
if (youHit) {
console.log("You hit!");
totalDamage += damageThisRound; <--- no closed c-bracket
if (totalDamage >= 4) {
console.log("You win!");
slaying = false;
} <---- closed c-braket
else {
youHit = Math.floor(Math.random() * 2);
}
} else {
console.log("You lose!");
slaying = false;
}
}


#15

you can have if statements inside of if statements for an infinite amount of times and with this assignment yes you are supposed to have an if inside of an if


#16

so I don't know whats happening, my code already passed and I just tried to run it but its giving me a syntax error. I think there might be a bug because I know my code passes and I don't see any syntax errors at all.


#17

This worked! I didn't know you could have an if with open c-bracket and without closed c-bracket.

Thank you!

var slaying = true
var youHit = Math.floor(Math.random() * 2)
var damageThisRound = Math.floor(Math.random() * 5 + 1)
var totalDamage = totalDamage + damageThisRound

while (slaying) {
if (youHit) {

	console.log("You hit!");
	totalDamage += damageThisRound;

	if (totalDamage >= 4) {
	console.log("You slew the dragon!")
	slaying = false;

	} else{
	    youHit = Math.floor(Math.random() * 2);
	    } 

	} else {
	console.log("you missed.");
	slaying = false;
}

}


#18

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