-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathranking.js
41 lines (33 loc) · 1.23 KB
/
ranking.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
let rankWords = (words, bonusLetters) => {
let rankings = words.map((wordInfo) => {
let bonusLetterIndices = [];
let encounteredLetters = new Map();
let giveScore = (word, bonusLetters) => {
let score = 0;
for (const letter of bonusLetters) {
// console.log("testing:", letter);
word.split("").forEach((wordLetter, index) => {
if (wordLetter === letter) {
bonusLetterIndices.push(index);
if (!encounteredLetters.has(letter)) {
score++;
encounteredLetters.set(letter, true);
}
}
});
}
return score;
}
let score = giveScore(wordInfo.word, bonusLetters)
return { wordInfo, score, bonusLetterIndices };
});
rankings.sort((a, b) => b.score > a.score);
console.log("rankings:", rankings);
return rankings.map((rank) => {
return {
word: rank.wordInfo.word,
combinationIndices: rank.wordInfo.combinationIndices,
bonusLetterIndices: rank.bonusLetterIndices
};
});
}