-
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 #6251 from almarro1/reto47-javascript
Reto #47 - javascript
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
Retos/Reto #47 - LA PALABRA DE 100 PUNTOS [Fácil]/javascript/almarro1.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,42 @@ | ||
|
||
function puntuacion(palabra) { | ||
return palabra | ||
//convertir a mayúsculas | ||
.toUpperCase() | ||
.replaceAll(/[ÁÀÄ]/g, 'A') | ||
.replaceAll(/[ÉÈË]/g, 'E') | ||
.replaceAll(/[ÍÌÏ]/g, 'I') | ||
.replaceAll(/[ÓÒÖ]/g, 'O') | ||
.replaceAll(/[ÚÙÜ]/g, 'U') | ||
// separar palabra en letras | ||
.split('') | ||
// eliminar todo lo que no sea A-Z | ||
.filter(letter => letter >= 'A' && letter <= 'Z') | ||
// pasar letra a valor según su código ascii | ||
.map(letter => letter.charCodeAt() - 64) | ||
.reduce((total, curr) => total + curr, 0); | ||
} | ||
|
||
const palabras = [ | ||
'matrimonio', 'tarántula', 'arizónico', 'zoológico', 'zoólogo', 'pensamiento' | ||
]; | ||
console.log('EJEMPLOS:') | ||
palabras.forEach(palabra => console.log(`${palabra} -> ${puntuacion(palabra)}`)); | ||
|
||
const readline = require('readline').createInterface({ input: process.stdin, output: process.stdout, prompt: 'guess>' }); | ||
|
||
async function main() { | ||
let found = false; | ||
while (!found) { | ||
const palabra = await new Promise(resolve => readline.question('Introduce una palabra: ', resolve)); | ||
const valor = puntuacion(palabra); | ||
console.log(`La palabra "${palabra}" tiene un valor de ${valor} puntos`); | ||
if (valor === 100) { | ||
console.log('¡Enhorabuena, has encontrado la palabra de 100 puntos!'); | ||
found = true; | ||
} | ||
} | ||
process.exit(); | ||
} | ||
|
||
main(); |