0

#1

Given a string of words, you need to find the word with the highest number of points.

Each letter of a word scores points according to it’s position in the alphabet. a=1, z=26 and everything inbetween. Write a function that will return the word with the highest number of points as a string. If two words score the same, return the word that appears earliest in the original string.All letters will be lower case and all inputs will be valid. There will be no special characters and all words will be separated by a space. For example
function example (‘peanut butter jelly time’) // will return ‘butter’ as the answer

#2

Spoiler
``````function findHighestPointWord(sentence) {
const words = sentence.trim().split(/\s+/);
const wordVals = [];

words.forEach(function(word) {
const lcWord = word.toLowerCase();
let wordVal = 0;
for (let i = 0; i < word.length; i++) {
wordVal += lcWord.charCodeAt(i) - 96;
}
wordVals.push(wordVal);
});
return words[wordVals.indexOf(Math.max(...wordVals))];
}
``````

#3

Hello ! Seem cool. Maybe a daily thing to do ? Will maybe take a look later to try out

#4

I’d love that too. I’m new to programming so you guys would have to help out also. Many Thanks

#5

Wait, are you hoping to make this into a series yourself, or just reposting a question you found elsewhere?

#6

Okay cool I’ll have that in mind. If they could be answered in different languages, it would be great

#7

I’ll make it a series

#8

Ah, I see.

I can help out with writing some tests to check answers, if you like

#9

Okay cool. no problemo

#10

If you intend this to be a challenge to others, you should also post your own work

Anyway here’s mine:

Summary
``````function score(word) {
return [...word].reduce((a, b) => a + b.charCodeAt() - 96, 0);
}

function wordWithHighestScore(words) {
const scores = words.map(score);
const highestScore = Math.max(...scores);

return words[scores.indexOf(highestScore)];
}

wordWithHighestScore(['peanut', 'butter', 'jelly', 'time']);
``````

#11

Lol, I see what you did there @kevcomedia

#12

OK, here are the tests:

https://jsfiddle.net/lionelrowe/ynwtoaa8/

To pass, your function must be named `findHighestPointWord()`, it must take one argument as a string (the sentence), and it must be case insensitive for input (output is case sensitive to the original string). However, it doesn’t need to handle special characters in any intelligent way (just the basic English-language alphabet and spaces).

#13

Lol I wasn’t reading the instructions . I thought the input would be an array of strings.

Here’s my revision:

Summary
``````function score(word) {
return [...word].reduce((a, b) => a + b.toLowerCase().charCodeAt() - 96, 0);
}

function findHighestPointWord(wordString) {
const words = wordString.split(' ');
const scores = words.map(score);
const highestScore = Math.max(...scores);

return words[scores.indexOf(highestScore)];
}
``````

#14

I think you should add this as a border case:

``````['ABC cba', 'ABC']
``````

or something like this

#15

Done and done!