I'm currently doing a simple dice roll app and I have the following object:
die = [
{
ofWhat: 6,
score: [6]
},
{
ofWhat: 6,
score: [1]
},
{
ofWhat: 6,
score: [5]
}
]
I would like to get every score and combine them into one value so I can see the total of every score from this array.
Like this: 6+1+5
How can I achieve this?
What I tried:
total = 0;
this.die.forEach((die) => {
die.score.forEach((score) => {
this.total += score;
});
});
I'm getting NaN
Edit: I made an error in my object
die = [
{
ofWhat: 6,
score: [6]
},
{
ofWhat: 6,
score: [1]
},
{
ofWhat: 6,
score: [5]
}
]
total = 0;
this.die.forEach((die) => {
die.score.forEach((score) => {
this.total += score;
});
});
console.log(total)
this.total += score;not betotal += score;?die.score.forEachshould throw an error