Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const colors = ['R', 'A', 'M', 'V','N', 'I'];
- const tit = `Descubre el codigo secreto | Juego`;
- const instructions = `Instrucciones:
- Descubre el codigo secreto insertando la letra
- del color correspondiente en el orden correcto`;
- const ask = `Inserta el codigo del color conformado por 4 letras.
- Colores:
- Rojo -> R, Azul -> A, Amarillo -> M, Verde -> V, Naranja -> N, Violeta -> I`;
- function generateSecurityCode(colors) {
- let res = '';
- function random() {
- return Math.floor(Math.random() * colors.length);
- }
- for (let i=0; i<4; i++) {
- res+=colors[random()];
- }
- return res;
- }
- function matchSecurityCode(code) {
- if (code.length === 4) {
- return true;
- } else {
- return false;
- }
- }
- function compareSecurityCode(gen, input) {
- let X = '';
- let Asterisc = '';
- const arr = gen.split('');
- if (gen === input) {
- return "Has adivinado el codigo secreto!";
- }
- arr.forEach((item, index) => {
- if (input.match(item)) {
- if (input[index] === arr[index]) {
- X+='X';
- } else {
- Asterisc+='*';
- }
- }
- });
- return (X + Asterisc) || `No acertaste ningun color. Fin del Juego.`;
- }
- function startApp() {
- alert(tit);
- alert(instructions);
- const generated = generateSecurityCode(colors);
- let code = prompt(ask);
- code = String(code).toUpperCase();
- if (matchSecurityCode(code)) {
- alert("Resultado: " + compareSecurityCode(generated, code));
- } else {
- alert('Error, no has enviado los colores correctamente');
- }
- }
- startApp();
Add Comment
Please, Sign In to add comment