How to assign a score to the highest scoring word

I am trying to create a function that will take in a string and each word into separate arrays. Each letter in the sub-array will be split. Really what I am wanting to do is return the highest scoring word by assigning numeric values to each letter and returning the word that has the most. This is my code so far

function high(x){
  var wordArray = x.split(" ");
  var score = 0;
  wordArray = wordArray.map(word => word.split(""));

  var alphabet = {a: 1, b: 2, c:3, d:4, e:5, f:6, g:7, h:8}
  for(var i = wordArray[0]; i < wordArray.length; i++){
    score = score + alphabet[wordArray[i]]
  }
  return score;
}



high('Taxi drivers')

“Drivers” with the current code

This is a code wars challenge and so this is the purpose of the challege
```
Given a string of words, you need to find the highest scoring word.

Each letter of a word scores points according to its position in the alphabet: a = 1, b = 2, c = 3 etc.

You need to return the highest scoring word as a string.

If two words score the same, return the word that appears earliest in the original string.

All letters will be lowercase and all inputs will be valid.

```

This is something I am trying to work on. I’ve watched videos on the problem solving techniques and breaking a problem down into components. Like what I did here a few days ago


After watching many coding video tutorials and looking up solutions I am now realizing my problem solving skills are weak. When I try to break a problem down I get too caught up in thinking about the syntax. But I am working to get better at it.

1 Like

When I look at this problem and break it down to an algorithm, here is what I come up with:

I need to track both the current highest score of all words and the position of that word in the string.

I start by splitting the string by the space character to obtain the list of words, assigning a value of 0 to the current highest score and a value of 0 for the highest scoring word index (the first word).

Next, I look at each word and calculate a score for it. This calculation is done by looking at each letter and getting its value based on the assumption that an “a” is 1 and “z” is 26 and then create a running sum of these values.

Next, I compare the running sum of the word’s letters with the current highest score so far. If it is greater than the current highest score, I update the current highest score to be the running sum and update the position in the string to position of the current word in the string (use zero-index position).

Once I have looked at all the words, I return the word based on the position (the highest scoring word index).

That is it.

Next I code my algorithm two ways:

function high(str){
  let highestScore = 0;
  let highestScoreWordIndex = 0;
  const wordArray = str.split(" ");
  for(let i = 0; i < wordArray.length; i++){
    let currentWordScore = 0;
    for (let j = 0; j < wordArray[i].length; j++) {
      currentWordScore += wordArray[i][j].toUpperCase().charCodeAt() - 64;
    }
    if (currentWordScore > highestScore) {
      highestScore = currentWordScore;
      highestScoreWordIndex = i;
    }
  }
  return wordArray[highestScoreWordIndex];
}

or using a more functional approach:

function high(str) {
  const wordArray = str.split(" ");
  const highestScoreWordIndex = wordArray
    .reduce((highest, word, i) => {
      const currentWordScore = [...word]
        .reduce((wordScore, letter) => wordScore + letter.toUpperCase().charCodeAt() - 64, 0);
      return currentWordScore > highest.highestScore
        ? { highestScore: currentWordScore, highestIndex: i }
        : highest;
    }, { highestScore: 0, highestIndex: 0 }).highestIndex;
  return wordArray[highestScoreWordIndex];
}