Guest User

Untitled

a guest
Mar 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. const colors = ['R', 'A', 'M', 'V','N', 'I'];
  2.  
  3. const tit = `Descubre el codigo secreto | Juego`;
  4. const instructions = `Instrucciones:
  5. Descubre el codigo secreto insertando la letra
  6. del color correspondiente en el orden correcto`;
  7. const ask = `Inserta el codigo del color conformado por 4 letras.
  8. Colores:
  9. Rojo -> R, Azul -> A, Amarillo -> M, Verde -> V, Naranja -> N, Violeta -> I`;
  10.  
  11. function generateSecurityCode(colors) {
  12. let res = '';
  13. function random() {
  14. return Math.floor(Math.random() * colors.length);
  15. }
  16. for (let i=0; i<4; i++) {
  17. res+=colors[random()];
  18. }
  19. return res;
  20. }
  21.  
  22. function matchSecurityCode(code) {
  23. if (code.length === 4) {
  24. return true;
  25. } else {
  26. return false;
  27. }
  28. }
  29.  
  30. function compareSecurityCode(gen, input) {
  31. let X = '';
  32. let Asterisc = '';
  33. const arr = gen.split('');
  34.  
  35. if (gen === input) {
  36. return "Has adivinado el codigo secreto!";
  37. }
  38.  
  39. arr.forEach((item, index) => {
  40. if (input.match(item)) {
  41. if (input[index] === arr[index]) {
  42. X+='X';
  43. } else {
  44. Asterisc+='*';
  45. }
  46. }
  47. });
  48. return (X + Asterisc) || `No acertaste ningun color. Fin del Juego.`;
  49. }
  50.  
  51. function startApp() {
  52. alert(tit);
  53. alert(instructions);
  54. const generated = generateSecurityCode(colors);
  55. let code = prompt(ask);
  56. code = String(code).toUpperCase();
  57. if (matchSecurityCode(code)) {
  58. alert("Resultado: " + compareSecurityCode(generated, code));
  59. } else {
  60. alert('Error, no has enviado los colores correctamente');
  61. }
  62. }
  63.  
  64. startApp();
Add Comment
Please, Sign In to add comment