Created
March 1, 2017 15:49
-
-
Save ivandevp/7067746b192aec9cc2dc979cacda6a91 to your computer and use it in GitHub Desktop.
Immediately Invoked Function Expression
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
// IIFE | |
(function () { | |
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