I have to create this JS scrabble but I'm having problems adding up each values of the word to make a total points, for example if the word is 'hello' it should the sum will be 8.. And also I need my object to return the total value of the word, not the [object Object]
..
const scores = {
a: 1,
e: 1,
i: 1,
o: 1,
u: 1,
l: 1,
n: 1,
r: 1,
s: 1,
t: 1,
d: 2,
g: 2,
b: 3,
c: 3,
m: 3,
p: 3,
f: 4,
h: 4,
v: 4,
w: 4,
y: 4,
k: 5,
j: 8,
x: 8,
q: 10,
z: 10
}
const notValid = 'Input not valid:'
const validWord = 'Great, your point is:'
const singleValue = scores
function scrabble(letter) {
if (typeof letter !== 'string') {
return `${notValid} 0`
} else if (letter === '!' || letter === '.') {
return `${notValid} 0`
} else if (letter === ' ' || letter === '\t\n') {
return `${notValid} 0`
} else if (letter === ',' || letter === '?') {
return `${notValid} 0`
} else if (letter === ',' || letter === null) {
return `${notValid} 0`
} else {
return `${validWord} ${singleValue} `
}
}
function loopLetters(word) {
let result = 0
for (let i = 0; i <= word.length; i++) {
result += [word[i]]
console.log(scores[word[i]])
}
return result
}
console.log('--', loopLetters('hello'))
console.log(scrabble(loopLetters('hello')))```
My console.log results are:
4
1
1
1
1
undefined
-- 0hello
4
1
1
1
1
undefined
Great, your point is: [object Object]