Calculate the word with the biggest score

Write a function to calculate the word with the biggest score.

I was given below challenge for a possible software developer job and only below statement is provided and nothing else. It says ;

Using ES5 or ES6, write a function ‘wordScoreCalculator’ that calculates the word with the highest score using the system A = 1, B = 2, C = 3 D = 4, E = 5 etc. The string will only contain a single space between words and there will be no punctuation.

I want to clarify what should be the required outcome they are asking ?

Either calculate the word with highest score as E = 5 in this case or calculate the highest score of whole string as 15 in this case?

Many thanks

1 Like

In what case?
What is the string of characters?

In any case, you really should ask the recruiter or interviewer to clarify the requirements of the test.

Hi @camelcamper

Recruiter not replying and I think understanding these questions are also part of interview process. . My understanding is that they are asking to calculate the word with the highest score in this case is E = 5 from below characters.

A = 1, B = 2, C = 3, D = 4, E = 5,

What do you think?

It is my understanding that the value of E = 5 because it is the 5th letter of the alphabet

So your function should read a string of characters and return the word with the highest value.

e.g.
Consider the following string of four words: “I am a bee

The first word “I” should evaluate to 9
The second word “am” should evaluate to 14 (1 + 13)
The third word “a” should evaluate to 1
The fourth word “bee” should evaluate to 12 (2 + 5 + 5)

Therefore the function should return “14”, “am”, or “am has the highest score of 14

e.g.2
Consider the following string of two words: “ABCDE Z

The first word “ABCDE” should evaluate to 15
The second word “Z” should evaluate to 26

Therefore the function should return “26”, “Z”, or “Z has the highest score of 26

Does it make any difference if words are single letters like A B C D or AM YOU etc? I think its not making any difference but I only thinking about A B C D E string.

Any spaces should indicate the end/beginning of a new word.

If you have a string
str = "String of words"
you can do
arrayOfWords = str.split(" ")

Then arrayOfWords === ["String", "of", "words"]

Then it is a matter of calculating the word with the highest score/value and returning it

Hi @camelcamper , I came up this code , please let me know how can we optimise this.

function wordScoreCalculator(str){
var anum={
A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8, I: 9, J: 10, K: 11,
L: 12, M: 13, N: 14, O: 15, P: 16, Q: 17, R: 18, S: 19, T: 20,
U: 21, V: 22, W: 23, X: 24, Y: 25, Z: 26
}
if(str.length== 1) return anum[str] || ’ ‘;
return str.split(’’).map(wordScoreCalculator);
}

wordScoreCalculator(‘I AM A BEE’)

I put it in a codepen for you.

Nice function! It is a good start but it does not fulfill the requirements.

It is currently returning the value of each letter in the string, however it should return the highest scoring word and it’s value.