trying to increment simon game level in javaScript

Viewed 39

I am new to javaScript and trying to create a simon game in jS and want to increase level everytime nextSequence() is called

var started = false; //toggler
var level = 0;
$(document).on("keydown", function() {
  if (!started) {
    $("h1").text("Level 0")
    nextSequence()

    started = true;
  }
})


function nextSequence() {
  var randomNumber = Math.random() * 4;
  randomNumber = Math.floor(randomNumber);
  var randomChosenColor = buttonColors[randomNumber] //any one color
  gamePattern.push(randomChosenColor);

  $("#" + randomChosenColor).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100); // animate
  level = level + 1
  console.log(level)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

1 Answers

Assuming you have all of the necessary variables set (buttonColors). The variable level should be increased by one every key down, if you want to display the score through the h1 you would need to incorporate the 'score' variable into the text

so change $("h1").text("Level 0") to $("h1").text("Level " + level)

Related