Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Score for Scrabble-Java Script

Tags:

javascript

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.

like image 960
codingTia Avatar asked Jul 03 '26 23:07

codingTia


2 Answers

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);
like image 94
ktilcu Avatar answered Jul 05 '26 12:07

ktilcu


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))
like image 29
Nina Scholz Avatar answered Jul 05 '26 13:07

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!