Created
March 1, 2017 15:33
-
-
Save ivandevp/05176348b61e4b85df63ae2a65c89f45 to your computer and use it in GitHub Desktop.
Ejercicio de un quiz utilizando un arreglo de objetos
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
var preguntar = function (pregunta) { | |
var respuesta = prompt(pregunta); | |
return respuesta.toUpperCase(); | |
}; | |
var esRespuestaCorrecta = function (respuesta, respuestaCorrecta) { | |
if (respuesta === respuestaCorrecta) { | |
return true; | |
} else { | |
return false; | |
} | |
}; | |
var quiz = function (preguntas) { | |
var respuestasCorrectas = 0; | |
var longitud = preguntas.length; // 4 | |
for (var i = 0; i < longitud; i++) { | |
var respuestaUsuario = preguntar(preguntas[i].enunciado); | |
if (esRespuestaCorrecta(respuestaUsuario, preguntas[i].respuestaCorrecta)) { | |
respuestasCorrectas += 1; | |
} | |
} | |
return respuestasCorrectas; | |
}; | |
var preguntas = [ | |
{ | |
enunciado: "¿En cuántos países opera Laboratoria?", | |
respuestaCorrecta: "3" | |
}, | |
{ | |
enunciado: "¿Cuál es la capital de Perú?", | |
respuestaCorrecta: "LIMA" | |
}, | |
{ | |
enunciado: "¿Cuál es el baile típico chileno?", | |
respuestaCorrecta: "CUECA" | |
}, | |
{ | |
enunciado: "¿Cuál es la moneda de México?", | |
respuestaCorrecta: "PESO" | |
} | |
]; | |
// Realiza el proceso | |
var respuestasCorrectas = quiz(preguntas); | |
var respuestasIncorrectas = preguntas.length - respuestasCorrectas; | |
// Muestra el resultado | |
console.log(respuestasCorrectas, respuestasIncorrectas); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment