-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.3.ts
34 lines (29 loc) · 990 Bytes
/
3.3.ts
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
/*
Para una palabra dada, calculamos su puntuación en Scrabble®
*/
function computeScore(word: string): number {
const letters: string[] = word.toUpperCase().split('');
return letters.reduce((accum, curr) => accum += getPointsFor(curr), 0);
}
function getPointsFor(letter: string): number {
const lettersAndPoints: [string, number][] = [
['AEOIULNRST', 1],
['DG', 2],
['BCMP', 3],
['FHVWY', 4],
['K', 5],
['JX', 8],
['QZ', 10],
];
return lettersAndPoints.reduce((computedScore: number, pointsTuple: [string, number]) => {
const [letters, score]: [string, number] = pointsTuple;
if (letters.split('').find((ll) => ll === letter)) {
return computedScore += score;
}
return computedScore;
}, 0);
}
console.log('[Ejercicio 3.3]', `zoologico vale ${computeScore('zoologico')} puntos.`);
/*
1 Añadir anotaciones de tipo siempre que sea posible
*/