2

I just started studying Javascript and I'm trying to create a mini-game. I want to get the vocation of a character comparing if his health skill and attack skill are bigger than magic and speed, then he's a knight. But what I'm trying is not working.

if (hp1 && at1 > mg1 && sp1){
    document.querySelector('.badgesContainer .knight').style.display = 'block'
}
else{
    document.querySelector('.badgesContainer .druid').style.display = 'none'
    document.querySelector('.badgesContainer .knight').style.display = 'none'
    document.querySelector('.badgesContainer .paladin').style.display = 'none'
    document.querySelector('.badgesContainer .sorcerer').style.display = 'none'  
}
4
  • 4
    This hp1 && at1 > mg1 && sp1 actually means just: if hp is truthy and at1 is bigger than mg1 and sp1 is truthy. Commented Sep 17, 2019 at 23:58
  • What you probably want is: hp1 > mg1 && hp1 > sp1 && at1 > mg1 && at1 > sp1. Commented Sep 17, 2019 at 23:59
  • 1
    I'd add a vocation property. It makes things easier down the road. Commented Sep 18, 2019 at 0:01
  • 1
    It depends on the variable types. If they're numbers, you can do (based on your literal requirement): if ((hp1 + at1) > (mg1 + sp1)), which simply adds hp and attack, then checks if that's greater than the total for magic and speed. Commented Sep 18, 2019 at 0:04

1 Answer 1

2

You can add the hp1 and at1 then compare it to mg1 and sp1

if ( (hp1 + at1) > (mg1 + sp1)){
    document.querySelector('.badgesContainer .knight').style.display = 'block'
}
else{
    document.querySelector('.badgesContainer .druid').style.display = 'none'
    document.querySelector('.badgesContainer .knight').style.display = 'none'
    document.querySelector('.badgesContainer .paladin').style.display = 'none'
    document.querySelector('.badgesContainer .sorcerer').style.display = 'none'  
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.