Ask coding questions

← Back to all posts
Why isn't my if else statement working?
Hamburgr (18)

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 (911) [earned 5 cycles]
View Answer
Comments
hotnewtop
realTronsi (911)

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')
realTronsi (911)

@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.

realTronsi (911)

@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!');
}
Coder100 (17038)

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!');
}
RYANTADIPARTHI (6001)

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.

Hamburgr (18)

@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).

Hamburgr (18)

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