-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5968 from marcode24/challenge-47
Reto #47 - Javascript
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Retos/Reto #47 - LA PALABRA DE 100 PUNTOS [Fácil]/js/marcode24.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* La última semana de 2021 comenzamos la actividad de retos de programación, | ||
* con la intención de resolver un ejercicio cada semana para mejorar | ||
* nuestra lógica... ¡Hemos llegado al EJERCICIO 100! Gracias 🙌 | ||
* | ||
* Crea un programa que calcule los puntos de una palabra. | ||
* - Cada letra tiene un valor asignado. Por ejemplo, en el abecedario | ||
* español de 27 letras, la A vale 1 y la Z 27. | ||
* - El programa muestra el valor de los puntos de cada palabra introducida. | ||
* - El programa finaliza si logras introducir una palabra de 100 puntos. | ||
* - Puedes usar la terminal para interactuar con el usuario y solicitarle | ||
* cada palabra. | ||
*/ | ||
|
||
const readline = require('readline'); | ||
|
||
const letterAValue = 'a'.charCodeAt(0); | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
const getResume = (word) => [...word] | ||
.map((letter) => `${letter} = ${letter.charCodeAt(0) - letterAValue + 1}\n`) | ||
.join(''); | ||
|
||
function calculatePoints(word) { | ||
let points = 0; | ||
for (let i = 0; i < word.length; i++) { | ||
const letter = word[i]; | ||
if (letter >= 'a' && letter <= 'z') { | ||
points += letter.charCodeAt(0) - letterAValue + 1; | ||
} | ||
} | ||
return points; | ||
} | ||
|
||
function jugar() { | ||
rl.question('Introduce una palabra: ', (word) => { | ||
const points = calculatePoints(word.toLowerCase()); | ||
console.log(`\nPuntos de "${word}": ${points}`); | ||
console.log(`${getResume(word)}`); | ||
|
||
if (points === 100) { | ||
console.log('¡Felicidades! Has alcanzado 100 puntos.'); | ||
rl.close(); | ||
} else { | ||
jugar(); | ||
} | ||
}); | ||
} | ||
|
||
jugar(); | ||
|
||
// Visita mi repo en GitHub para ver y correr los tests de este código --> https://github.com/marcode24/weekly-challenges |