dj92
dj92

Reputation: 29

add card object values together

Im new to programming and Javascript and im a bit stuck on a school assignment atm. My code below works when i log one object like this:
score([{ suit: 'HEARTS', value: 1 }]) This logs 11 but if i want to log multiple objects at the same time and add their values together, how could i do that? For example, if i call these objects below with the values 2, 3 and 4 it should return 9.

score([{ suit: 'HEARTS', value: 2 }, { suit: 'HEARTS', value: 3 }, { suit: 'HEARTS', value: 4 }])

Here is my code:

let score = function (cardObject) {
  let getScore = 0;

  for (let i = 0; i < cardObject.length; i++) {
   getScore += cardObject[i].value
   
    if (getScore === 1) {
      return 11;
    } else if (getScore === 2) {
      return 2;
    } else if (getScore === 3) {
      return 3;
    } else if (getScore === 4) {
      return 4;
    } else if (getScore === 5) {
      return 5;
    } else if (getScore === 6) {
      return 6;
    } else if (getScore === 7) {
      return 7;
    } else if (getScore === 8) {
      return 8;
    } else if (getScore === 9) {
      return 9;
    } else {
      return 10;
    }
  }
}

Upvotes: 1

Views: 272

Answers (4)

Rafael R. Camargo
Rafael R. Camargo

Reputation: 43

For a simpler approach, that should do the trick:

function score(cardObject) {
  let getScore = 0

  for (let i = 0; i < cardObject.length; i++) {
    switch (cardObject[i].value) {
      case 1: {
        getScore += 11
        break
      }
      default: {
        getScore += cardObject[i].value
        break
      }
    }
  }

  return getScore
}

score([{suit:"HEARTS",value:1}]) // 11
score([{suit:"HEARTS",value:2},{suit:"HEARTS",value:3},{suit:"HEARTS",value:4}]) // 9

Upvotes: 0

imvain2
imvain2

Reputation: 15857

Since your if statements are covering every number, you don't need an if statement for all of them.

Updated the function per comments to include if statement to take into consideration face cards.

The first if statement checks for values greater than and equal to 10 and lower than and equal to 13. If so add 10. If card value is 1, add 11. Else add the card's value.

Then I simply return the getStore variable.

Your code was returning the value, which ends the function without calling it again. The return in this case would be the last thing after all of the logic was complete.

let score = function(cardObject) {
  let getScore = 0;

  for (let i = 0; i < cardObject.length; i++) {
    let cardValue = cardObject[i].value;
    if (cardValue >= 10 && cardValue <= 13) {
      getScore += 10;
    } else if (cardValue === 1) {
      getScore += 11;
    } else {
      getScore += cardValue;
    }
  }

  return getScore;
}

console.log(score([{
  suit: 'HEARTS',
  value: 10
}, {
  suit: 'HEARTS',
  value: 11
}, {
  suit: 'HEARTS',
  value: 12
}]));

Upvotes: 2

Temmie19
Temmie19

Reputation: 33

You actually don't need to do if num return num to get the number. But what's actually happening here is the use of the return function. Basically, it completely exits the function and gives the value back. So what you would really need to do instead is something like this

let score = function (cardObject) {
  let getScore = 0;

  for (let i = 0; i < cardObject.length; i++) {
    //Instead of making it so that we return a bunch of numbers, we check to see if that
    //value from the cardObject array is a 1, and if it is add an 11 to getScore,
    //otherwise, add that number to getScore
    if (cardObject[i].value == 1){
      getScore += 11;
    } else {
      getScore += cardObject[i].value;
    }
  }
  //And then finally we return getScore instead
  return getScore;
}

Upvotes: 0

Unmitigated
Unmitigated

Reputation: 89414

You can use Array#reduce.

function score(arr) {
   return arr.reduce((acc, {value}) => acc + (value === 1 ? 11 : value), 0);
}
let res = score([{ suit: 'HEARTS', value: 2 }, { suit: 'HEARTS', value: 3 }, { suit: 'HEARTS', value: 4 }]);
console.log(res);

Upvotes: 1

Related Questions