Skip to content
Sign upLog in
← Back to Community

Why isn't my if else statement working?

Profile icon
Hamburgr

Currently, I am experimenting with JS however, I am having a problem with my code.

I want to make an if else statement that when the user says attack or run, it will say the corresponding console.log statement. However, whenever I test out writing run or Run it says the corresponding console.log for whenever the user is supposed to write attack. Can anyone help me please?

Here is my code:

var damageDealt = Math.floor((Math.random() * 10) + 1); console.log('A Challenger approaches!') console.log('1. Attack') console.log('2. Run') var userOption = prompt('What would you like to do?: ') if (userOption === 'Attack' || 'attack') { console.log('You chose to attack! You deal ' + damageDealt + ' damage!') } else if (userOption != 'Attack' || 'attack') { console.log('You ran away!'); }
Answered by realTronsi [earned 5 cycles]
View Answer
Voters
Profile icon
AkhilReddy25
Profile icon
LandofStorie
Profile icon
Hamburgr
Comments
hotnewtop
Profile icon
realTronsi

You're doing the "OR" wrong

if (userOption === 'Attack' || 'attack')

instead of that do this:

if (userOption === 'Attack' || userOption === 'attack')

but to simplify it, you can just use .lower

if (userOption.toLowerCase() === 'attack')
Profile icon
realTronsi

@realTronsi
when you do what you did, what it is essentially saying is

if userOption == Attack is true, OR the string 'attack' is true, since it automatically treats the OR as a new expression.

Profile icon
realTronsi

@realTronsi
so change this:

ar damageDealt = Math.floor((Math.random() * 10) + 1); console.log('A Challenger approaches!') console.log('1. Attack') console.log('2. Run') var userOption = prompt('What would you like to do?: ') if (userOption === 'Attack' || 'attack') { console.log('You chose to attack! You deal ' + damageDealt + ' damage!') } else if (userOption != 'Attack' || 'attack') { console.log('You ran away!'); }

** into this:**

ar damageDealt = Math.floor((Math.random() * 10) + 1); console.log('A Challenger approaches!') console.log('1. Attack') console.log('2. Run') var userOption = prompt('What would you like to do?: ') if (userOption.toLowerCase() === 'attack') { console.log('You chose to attack! You deal ' + damageDealt + ' damage!') } else { console.log('You ran away!'); }
Profile icon
Coder100

Your if statement becomes:

if ((userOption === 'Attack') || 'attack') {

so that is always true because 'attack' is always true. Change your code to:

var damageDealt = Math.floor((Math.random() * 10) + 1); console.log('A Challenger approaches!') console.log('1. Attack') console.log('2. Run') var userOption = prompt('What would you like to do?: ') if (userOption === 'Attack' || userOption === 'attack') { console.log('You chose to attack! You deal ' + damageDealt + ' damage!') } else if (userOption !== 'Attack' && userOption !== 'attack') { console.log('You ran away!'); }
Profile icon
RYANTADIPARTHI

You should do this:

if (userOption === 'Attack' || userOption === 'attack'){ console.log('You chose to attack! You deal ' + damageDealt + ' damage!') } else if (userOption != 'Attack' || 'attack') { console.log('You ran away!'); }

You are just putting ||, but not calling it again.

Profile icon
Hamburgr

@Coder100
@realTronsi
@RYANTADIPARTHI
Thanks for helping me out! However I have tried all of your answers and I am having a different but similar issue with all of them. Now, whenever I do attack, it shows the console.log statement for the run answer (Note: the run doesn't have any issues).

Profile icon
realTronsi

@Hamburgr
it works for me

Profile icon
Hamburgr

@realTronsi
oh ya I also just realized, sorry about that.