forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e822473
commit fc6c4e0
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
Retos/Reto #7 - EL SOMBRERO SELECCIONADOR [Media]/javascript/BRivasTorres.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,80 @@ | ||
const readline = require("readline"); | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
function askQuestion(question, options) { | ||
return new Promise((resolve) => { | ||
const formattedOptions = options | ||
.map((option, index) => `${index + 1}. ${option}`) | ||
.join("\n"); | ||
rl.question( | ||
`${question}\n${formattedOptions}\nRespuesta: `, | ||
(answer) => { | ||
const selectedOption = parseInt(answer); | ||
if ( | ||
isNaN(selectedOption) || | ||
selectedOption < 1 || | ||
selectedOption > options.length | ||
) { | ||
console.log( | ||
"Respuesta no válida. Por favor, selecciona una opción válida." | ||
); | ||
resolve(askQuestion(question, options)); | ||
} else { | ||
resolve(selectedOption); | ||
} | ||
} | ||
); | ||
}); | ||
} | ||
|
||
async function sortingHat() { | ||
const questions = [ | ||
"¿Qué cualidad valoras más?", | ||
"En una situación difícil, ¿qué harías?", | ||
"¿Qué tipo de amigos prefieres?", | ||
"Elige una criatura mágica:", | ||
"¿Cuál es tu materia favorita en Hogwarts?", | ||
]; | ||
|
||
const options = [ | ||
["Coraje", "Astucia", "Lealtad", "Intelecto"], | ||
[ | ||
"Enfrentarlo directamente", | ||
"Analizar la situación", | ||
"Pedir ayuda", | ||
"Evitar el problema", | ||
], | ||
[ | ||
"Ambiciosos y decididos", | ||
"Inteligentes y creativos", | ||
"Leales y amigables", | ||
"Honestos y sabios", | ||
], | ||
["Dragón", "Serpiente", "Hippogrifo", "Elfo doméstico"], | ||
[ | ||
"Defensa contra las artes oscuras", | ||
"Adivinación", | ||
"Herbología", | ||
"Transformaciones", | ||
], | ||
]; | ||
|
||
let housePoints = [0, 0, 0, 0]; | ||
|
||
for (let i = 0; i < questions.length; i++) { | ||
const answer = await askQuestion(questions[i], options[i]); | ||
housePoints[answer - 1]++; | ||
} | ||
|
||
const maxPointsIndex = housePoints.indexOf(Math.max(...housePoints)); | ||
const houses = ["Gryffindor", "Slytherin", "Hufflepuff", "Ravenclaw"]; | ||
console.log(`¡Bienvenido a ${houses[maxPointsIndex]}!`); | ||
|
||
rl.close(); | ||
} | ||
|
||
sortingHat(); |