Skip to content

Commit 1cc27a8

Browse files
committed
Re-do kata
1 parent d637cff commit 1cc27a8

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed
Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
// FIXME: types is ringing
2-
export function high(str) {
3-
// 1. We split the string into an array of words
4-
const wordsArray = str.split(" ");
5-
// 2. We split each word into letters
6-
const lettersArray = wordsArray.map(word => word.split(""));
7-
// 3. We find the points per letter and put it in an array
8-
let numbersArray = [];
9-
for (let word of lettersArray) {
10-
numbersArray = word
11-
// 3.2. We are using charCodeAt to assign the score per letter (i.e. charCodeAt(a) = 97 then 97-1 => a = 1 point
12-
.map(letter => letter.charCodeAt(0) - 96)
13-
// 3.3. We sum all the points
14-
// The 0 at the end of reduce is giving an initial value of 0 to the reduce function since in one single mf case it gave error
15-
.reduce((total, amount) => total + amount, 0);
16-
numbersArray.push(numbersArray);
17-
}
18-
// 4. Compare the points for each word and return the highest
19-
const winnerIndex = numbersArray.indexOf(Math.max(...numbersArray));
20-
// 6. Return the word associated
21-
return wordsArray[winnerIndex];
1+
export function high(str: string): string {
2+
const wordsArray: string[] = str.split(' ');
3+
const lettersArray: string[][] = wordsArray.map(word => word.split(""));
4+
5+
let scoresArray = lettersArray.map(word => word.map(letter =>
6+
letter.charCodeAt(0)- 96).reduce((total, amount) => total + amount));
7+
8+
const scoreTable = wordsArray.reduce((object, key, index) => {
9+
return {
10+
...object,
11+
[key]: scoresArray[index]
12+
};
13+
}, {});
14+
const highestScore = Math.max(...scoresArray);
15+
return Object.keys(scoreTable).find(word => scoreTable[word] === highestScore) || '';
2216
}

0 commit comments

Comments
 (0)