0

I'm working on my first javascript project and can't figure out what is wrong with this function.

function calculateAttackValue(deck) {

    for (let i = 0; i < deck.length; i++) {

        if (deck[i].Value == 'two' || 'three' || 'four' || 'five' || 'six' || 'seven' || 'eight' || 'nine' || 'ten') {
            deck[i].attackValue = wordsToNumbers(deck[i].Value)
        } else if (deck[i].Value == 'king' || 'queen' || 'jack') {
            deck[i].attackValue = 10
        } else {
           deck[i].attackValue = 0
        }
    }
};

What I intended to happen was for each card with a value of "king" "queen" or "jack" to have an attack value of 10 and a value of "ace" have an attack value of 0. The first part of the if statement works and assigns the attack value property correctly but the rest are returning the strings of "king""queen""jack" and "ace" instead of the intended 10s and 0.

What have I done wrong?

Donjoeker
  • 1
  • 1
  • You ain't compairing the second values. I think you want the check if it's in the list. Therefore you can create an array and perform a contains on the array for the value. – StuiterSlurf Jul 21 '20 at 10:58
  • Your boolean expression isn't correct. `if (deck[i].Value == 'two' || 'three')` will *always* evaluate to true. Try this for yourself in the browser console by evaluating the expression `if (false || 'three') { console.log('true'); }`. You have to check if `deck[i].Value == 'two' || deck[i].Value == 'three'` etc.. – nbokmans Jul 21 '20 at 10:59
  • You have a function `wordsToNumbers` - can't you just update that function to map `'king'`, `'queen'`, `'jack'` to `10`? Sounds like it'd be more efficient. – Niet the Dark Absol Jul 21 '20 at 11:05
  • Thanks a lot for the help! @NiettheDarkAbsol I did think of doing this but I wanted to know what I'd done wrong rather than just find a work around. – Donjoeker Jul 21 '20 at 11:14

0 Answers0