I’m having trouble updating the score property. I’m fairly new but still feel crazy not being able to figure this out.
‘fight’ is a string
JavaScript
x
29
29
1
function alphabetWar(fight) {
2
let leftSide = {
3
'w': 4,
4
'p': 3,
5
'b': 2,
6
's': 1,
7
'score': 0
8
}
9
10
let rightSide = {
11
'm': 4,
12
'q': 3,
13
'd': 2,
14
'z': 1,
15
'score': 0
16
}
17
18
for (let char of fight) {
19
if (leftSide.hasOwnProperty(char)) {
20
leftSide.score += leftSide.char;
21
if (rightSide.hasOwnProperty(char)) {
22
rightSide.score += rightSide.char;
23
}
24
}
25
}
26
console.log(leftSide.score)
27
if (leftSide.score === rightSide.score) return "Let's fight again!";
28
return leftSide.score > rightSide.score ? 'Left side wins!' : 'Right side wins!';
29
}
Advertisement
Answer
You can try:
JavaScript
1
9
1
for (let char of fight.split('')) {
2
if(typeof leftSide[char] != 'undefined'){
3
leftSide.score += leftSide[char]
4
}
5
if(typeof rightSide[char] != 'undefined'){
6
rightSide.score += rightSide[char]
7
}
8
}
9