codingTia
codingTia

Reputation: 31

Calculating Score for Scrabble-Java Script

I'm trying to calculate the Scrabble score of a word. But each time I type in a word-it returns the sequence of numbers each letter is associated with. For instance, when I type if fox for my scrabble word I get 418. Why is this happening?

const oldScoreKey = {
  1: ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'],
  2: ['D', 'G'],
  3: ['B', 'C', 'M', 'P'],
  4: ['F', 'H', 'V', 'W', 'Y'],
  5: ['K'],
  8: ['J', 'X'],
  10: ['Q', 'Z']
};


function transform(oldScoreKey){
  const newScoreKey = {};
  for (const [letterValue, letterArr] of Object.entries(oldScoreKey)) {
    for (const letter of letterArr) {
      newScoreKey[letter.toLowerCase()] = letterValue;
    }
  }
  return newScoreKey;
}

console.log(transform(oldScoreKey));


// Code your initialPrompt function here:
const input = require('readline-sync');

console.log("Using algorithm: Scrabble");

let word = (input.question("Enter a simple word please: "));
const alphabet = "abcdefghijklmnopqrstuvwxyz";


function simpleScore() {
  let score = 0

  for(let letter of word.toLowerCase()){
    if (alphabet.includes(letter))
    score += 1;
  }
  return score;
}

console.log(simpleScore())


let letter = (input.question("Enter a scrabble word please: "));
letter = letter.toLowerCase();


let newAlphabet = { 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' }
function scrabbleScore() {
let sum = 0
let i = 0
let score = 0
for (i = 0; i < word.length; i++) {
    letter = word[i];
    sum += newAlphabet[letter];
}
return (sum*1);
}
console.log(scrabbleScore())

When the Scrabble Score prints I am trying to get the total number of 418, which would equal 13 points.

Upvotes: 3

Views: 4359

Answers (5)

Scott Sauyet
Scott Sauyet

Reputation: 50797

Here is how I would break this down.

This version includes a cleaner version of the code to build the new score key. (It depends upon flatMap which isn't yet universal, but which is easy to shim, and which could be replaced easily enough with a reduce if desired.)

const invertScoreKey = (old) =>
  Object.assign (...Object .entries (old) .flatMap (
    ([k, arr]) => arr .map ((v) => ({[v .toLowerCase ()]: Number (k)}))
  ))

const sum = (ns) => ns .reduce((total, n) => total + n, 0)

const scoreWord = (scores) => (word) => 
  sum ([...word .toLowerCase()] .map(c => scores[c] || 0))

const oldScoreKey = {
  1: ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'],
  2: ['D', 'G'],
  3: ['B', 'C', 'M', 'P'],
  4: ['F', 'H', 'V', 'W', 'Y'],
  5: ['K'],
  8: ['J', 'X'],
  10: ['Q', 'Z']
}

const scrabbleScore = scoreWord (invertScoreKey (oldScoreKey))

'The quick, brown fox jumped over the lazy dog'.split(/\W+/)
  .forEach(word => console.log(`${word} : ${scrabbleScore(word)}`)
)

Note that scoreWord is generic, accepting a score key and returning a function which takes a word and returns the total score by that key. scrabbleScore is based on this, using the result of inverting the original key as its key.

Upvotes: 0

Leroy Alonzo
Leroy Alonzo

Reputation: 203

Because it's not convert to integer. I will put code from your code.

function scrabbleScore() {
    let sum = 0;
    let i = 0;
    let score = 0;
    for (i = 0; i < word.length; i++) {
        letter = word[i];
        sum += parseInt(newAlphabet[letter]);
    }
    return (sum*1);
}
console.log(scrabbleScore());

This will help you.

Upvotes: 0

ktilcu
ktilcu

Reputation: 3128

The scores should be numbers. Here is an easy way to represent this operation.

const newAlphabet = {
  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 scrabbleScore = word =>
  word
    .split('')
    .map(letter => newAlphabet[letter])
    .reduce((a, b) => a + b);

Upvotes: 3

Nina Scholz
Nina Scholz

Reputation: 386654

You could take an object with numbers and a default value of zero, if the character does not exist in the object.

function scrabbleScore(word) {
    let newAlphabet = { 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 },
        sum = 0,
        i;

    word = word.toLowerCase();
    for (i = 0; i < word.length; i++) {
        sum += newAlphabet[word[i]] || 0; // for unknown characters
    }
    return sum;
}

let letter = prompt("Enter a scrabble word please: ");

console.log(scrabbleScore(letter))

Upvotes: 1

MR.QUESTION
MR.QUESTION

Reputation: 359

In JS += is ok for strings and it not parse value to int but just concatenate. Try to force it manually:

sum += parseInt(newAlphabet[letter]);

Upvotes: 0

Related Questions