-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhigh.js
More file actions
28 lines (19 loc) · 712 Bytes
/
Copy pathhigh.js
File metadata and controls
28 lines (19 loc) · 712 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const fn = require('./fn')
const charCode = char => char.charCodeAt(0)
const CHAR_CODE_TO_SCORE_DISTANCE = charCode('a') - 1
const getLetterScore = letter => charCode(letter) - CHAR_CODE_TO_SCORE_DISTANCE
const getWordScore = fn.pipe(fn.toCharArray, fn.map(getLetterScore), fn.sum)
const toScoredWord = word => ({ word, score: getWordScore(word) })
const getMaxScoredWord = fn.reduce((currentWord, nextWord) =>
currentWord === undefined || nextWord.score > currentWord.score
? nextWord
: currentWord
)
const pickWord = scoredWord => scoredWord.word
const highestScoringWord = fn.pipe(
fn.getWords,
fn.map(toScoredWord),
getMaxScoredWord,
pickWord
)
module.exports = highestScoringWord