Weird javascript bug, nostalgic

Hey pops I’m not the most active user here but then again o/

I was working on the simon’s game and it worked just a few mins ago, and then I tried to do something and basically just reverted back to the original script (the experiment I was doing is commented in the script below) why is the confirm() function not working? and why is the game not starting?

CODE


var colors = ["red", "blue", "green", "yellow"];
var currentState = [];
var generateState = function(){
  var randomColor = colors[Math.floor(Math.random() * colors.length)];
  currentState.push(randomColor);
}
var gameStep = 0;

var resetState = function(){
  currentState = [];
  gameStep = 0;
}

var renderState = function(msg){
  alert(msg);
/*
  for(var i =0; i < currentState.length; i++){
    setTimeOut(function(){
      document.getElementById(currentState[i]).style.background = white;
    }, 1000)
  }
*/
}

var checkMatch = function(currentState, userState){
  var match = false;
  console.log(userState,currentState);
  for(var i=0; i < currentState.length; i++){
    if(currentState[i] === userState[i]){
      match = true;
    } else{
      match = false;
    }
  }
  return match;
}

var isCorrect = function(bool){
  if(bool){
    gameStep++;
    initialPrompt(true);
  } else{
    renderState("Hmm you must have forgotten the current state is " + currentState);
    var answer = prompt("Hey, what did I show you right now?").split(',');
    var correct = checkMatch(currentState, answer);
    isCorrect(correct);
  }
}

var initialPrompt = function(recursive){
  if(recursive===false){
    var firstprompt = confirm("heya, would you like to play a game?");
    if(firstprompt == true){
      generateState();
      renderState("Current state is " + currentState);
      var answer = prompt("Hey, what did I show you right now?").split(',');
      var correct = checkMatch(currentState, answer);
      isCorrect(correct);
    }
  } else if(recursive === true) {
    if(gameStep < 5){
      generateState();
      renderState("Current state is " + currentState);
      var answer = prompt("Hey, what did I show you right now?").split(',');
      var correct = checkMatch(currentState, answer);
      isCorrect(correct);
    } else{
      resetState();
      initialPrompt(false);
    }
  }
}
initialPrompt(false);

I fixed it! but I don’t know how I fixed it, but I fixed it… wtf why is this such a mystery!!!

cansomeone tell what’ wrong with it?

No you must be mistaken check again here’s a :cake: for trying to help

1 Like