Drill

ProblemsTypeScript › games

Score a word by letter positions

easygamesTypeScript

A word game scores each letter by its position in the alphabet: a is 1, b is 2, and so on up to z is 26.

wordScore(word: string) → int

Solve it in the editor →

Where you start

function wordScore(word: string): number {
  
}

Worked examples

CallResult
wordScore("abc")6
wordScore("hello")52
wordScore("")0
wordScore("a")1

Hint

Subtract the character code of a from each letter to get its value.

Reference solution in TypeScript
function wordScore(word: string): number {
  let sum = 0;
  for (let i = 0; i < word.length; i++) {
    const c = word.charCodeAt(i);
    if (c >= 97 && c <= 122) sum += c - 96;
  }
  return sum;
}

The same problem in another language

More games problems in TypeScript