jeudi 25 août 2016

color guess game by if-statement

i want to know how to make the color guess game with if and else if instead of while loop? i had tried by doing this code but it work only 2 times first and then the second prompt appear then everything is gone. so i need help in making it accept infinite times and work well

var color = ['black', 'yellow', 'blue', 'red'];
var theAnswer = color[Math.floor(Math.random() * color.length)];
var  type;
var ind;
var i = 0;
var input1;
function popPrompt () {
   prompt("sorry try again\nguess one color\n" + color);
   
  
}
function guess () {
   alert(theAnswer);
   input1  = prompt("guess one color\n" + color);
   ind = color.indexOf(input1);
   if (ind == -1) {
    alert('please enter one of the above colors');
    i = i + 1;
    popPrompt();

   }
    if (ind != -1 && input1 > theAnswer) {
    alert('your answer has a higher alphapetical');
    i = i + 1;
    popPrompt();

   }
 if (ind != -1 && input1 < theAnswer) {
    alert('your answer has a lower alphapetical');
    i = i + 1;
    popPrompt();

   }
    if (ind != -1 && input1 == theAnswer) {
    alert('your answer is correct\n you did it in' + i +" times");
    document.body.style.backgroundColor = theAnswer ;
    i = i + 1;
    

   }

}

Aucun commentaire:

Enregistrer un commentaire