Hello, I am currently new to programming and learning Javascript. I am facing a challenge with a school assignment at the moment. In my code below, I have a function that works when I log one object like this:
score([{ suit: 'HEARTS', value: 1 }])
This returns 11. However, I am unsure how to modify my code to handle multiple objects and add their values together for a combined score. For instance, if I call these objects with values 2, 3, and 4, it should return 9.
score([{ suit: 'HEARTS', value: 2 }, { suit: 'HEARTS', value: 3 }, { suit: 'HEARTS', value: 4 }])
The current implementation of my code is as follows:
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;
}
}
}