Reputation: 13206
letter = prompt("Enter a word please");
letter = letter.toUpperCase();
// define letters and respective scores
alphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q',
'R','S','T','U','V','W','X','Y','Z']
alphabetScore = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10]
// start sum
sum=0
for (i=0; i<alphabet.length; i++)
{
switch(letter)
{
case (alphabet[i]): sum+=alphabetScore[i]; break;
default: sum=sum+0;
}
}
alert (sum);
I'm trying to build a program that calculates the Scrabble score of a word. But each time I type in a word - it returns 0. Why is this happening? Sorry for the continuous JS posts - I'm new to the language and have been take extra practical assignments to boost my knowledge of the language. In this case I've been experimenting with the switch statement within a for loop.
Upvotes: 1
Views: 5564
Reputation: 335
let word = prompt("Enter a word please");
const scrabble = {
a: 1, b: 3, c:3, d: 2, e: 1, f: 4, g: 2, h:4, i:1, j: 8,
k: 5, l: 1, m: 3, n: 1, o: 1, p: 3, q: 10, r: 1, s: 1, t: 1,
u: 1, v: 4, w: 4, x: 8, y: 4, z: 10
};
const sum = [...word].reduce((accu, letter) => { return accu + scrabble[letter.toLowerCase()]; }, 0);
alert(sum);
Upvotes: 1
Reputation: 4937
Try modeling the relationship between a letter and its Scrabble point value as an associative array rather than as two parallel arrays:
var word = prompt("Enter a word please");
word = word.toUpperCase();
scores = { 'A' : 1, 'B' : 3, 'C' : 3, /* ... */ 'Z' : 10 };
var sum = 0;
for (var i = 0; i < word.length; ++i) {
sum += scores[word.charAt(i)] || 0;
}
alert(sum);
Upvotes: 2
Reputation: 4210
You could avoid looping over the entire alphabet for each letter in the word by defining the scores in the form of an object (a.k.a. associative array). Also, your "letter" variable is actually the whole word, so you'd need to loop through the letters individually. The following combines these two ideas:
var word = prompt("Enter a word please");
word = word.toUpperCase();
var alphabet = {
A: 1,
B: 3,
C: 3,
D: 2,
E: 1,
F: 4,
G: 2,
H: 4,
I: 1,
J: 8,
K: 5,
L: 1,
M: 3,
N: 1,
O: 1,
P: 3,
Q: 10,
R: 1,
S: 1,
T: 1,
U: 1,
V: 4,
W: 4,
X: 8,
Y: 4,
Z: 10
}
var letter, i, sum = 0;
for (i = 0; i < word.length; i++) {
letter = word[i];
sum += alphabet[letter];
}
alert(sum);
Upvotes: 2
Reputation: 41533
sum = 0;
for(var i=0,l=letter.length;i<l;i++)
sum += alphabetScore[alphabet.indexOf(letter[i])]
Upvotes: 1
Reputation: 14179
That's not the appropriate way of using switch.
switch
is commonly used to replace several consequential if ... else if ... else if ... else
. It's as simple as that. In your case you just need a single if
statement inside the loop. And you actually need a double loop.
for each letter in the word
for each letter in the alphabet
if they match
increment score
Good luck.
Upvotes: 2
Reputation: 183361
You're treating the entire word like it's a single letter, and trying to find where in the alphabet it is. Since it's not in the alphabet at all, you get zero. Instead, you need to loop over the letters of the word.
Upvotes: 1
Reputation: 81694
Maybe take a closer look at the line
switch(letter)
Yes?
Upvotes: 1