1

I need help in my code by the way its a typing game that has timer and a life and a scoreboard im also a beginner in JavaScript my problem here is i want to snuffle again the word after the user got the answer correct or got the wrong answer sorry for my english

var words = ["PRINTF","SCANF","GETCH","MAIN"];
    var life;
    var seconds;
    var temp;
    var score=0;
    //Timer
    function countdown() {
    seconds = document.getElementById('countdown').innerHTML;
    seconds = parseInt(seconds, 10);
    if (seconds == 0) {
      temp = document.getElementById('countdown');
      alert("TIME IS UP!");
      document.getElementById("countdown") = 0;
    }
    seconds--;
    temp = document.getElementById('countdown');
    temp.innerHTML = seconds;
    timeoutMyOswego = setTimeout(countdown, 1000);
    }
   
    //Random
    var getRandomWord = function () {
    return words[Math.floor(Math.random() * words.length)];
    }
    var word = getRandomWord();  
    //Display
    function start(){  
    document.getElementById("display").innerHTML = word;   
    }

    // Validate the entered word
    function go() {
    var input = document.getElementById("demo").value;
    var input1 = input.toUpperCase();
     
    if (word==input1){
       alert('Correct');
       score=score+1;
       document.getElementById("score").innerHTML = score;
       start();
    }
    else 
    {
        alert('Wrong');
        start();
        life = document.getElementById('life').innerHTML;
        life = parseInt(life);
        life = life-1;
        document.getElementById("life").innerHTML = life;    
    }
    }
<body onLoad = "countdown(); start();">
    Time: <span id="countdown" style="font-weight: bold;">11</span><br>
    Life: <span id="life" style="font-weight: bold;">3</span><br>
    Score:<span id="score" style="font-weight: bold;">0</span><br>
    WORD: <span id="display"></span>
    <section id="f1">
    <input type="text" id="demo"  onkeydown="if (event.keyCode == 13) go();" >
    </section>
    </body>
Kingsgame
  • 15
  • 2

1 Answers1

0

Your randomization method always returns 0th element from words array. Try below snippet:

var word;
var words = ["PRINTF", "SCANF", "GETCH", "MAIN"];
var life;
var seconds;
var temp;
var score = 0;
//Timer
function countdown() {
  seconds = document.getElementById('countdown').innerHTML;
  seconds = parseInt(seconds, 10);
  if (seconds == 0) {
    temp = document.getElementById('countdown');
    alert("TIME IS UP!");
    document.getElementById("countdown") = 0;
  }
  seconds--;
  temp = document.getElementById('countdown');
  temp.innerHTML = seconds;
  timeoutMyOswego = setTimeout(countdown, 1000);
}

//Random
var getRandomWord = function() {
  return shuffle(words)[0];
}

//Display
function start() {
  word = getRandomWord();
  document.getElementById("display").innerHTML = word;
}

// Validate the entered word
function go() {
  var input = document.getElementById("demo").value;
  var input1 = input.toUpperCase();

  if (word == input1) {
    alert('Correct');
    score = score + 1;
    document.getElementById("score").innerHTML = score;
    words.splice(words.indexOf(word), 1);
    document.getElementById("demo").value = "";
    if (words.length) {
      start();
    } else {
      alert('over');
    }
  } else {
    alert('Wrong');
    document.getElementById("demo").value = "";
    if (words.length) {
      start();
    } else {
      alert('over');
    }
    life = document.getElementById('life').innerHTML;
    life = parseInt(life);
    life = life - 1;
    document.getElementById("life").innerHTML = life;
  }
}


//Ref: http://stackoverflow.com/a/2450976/1548301
function shuffle(array) {
  var currentIndex = array.length,
    temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}
<body onLoad="countdown(); start();">
  Time: <span id="countdown" style="font-weight: bold;">11</span>
  <br>Life: <span id="life" style="font-weight: bold;">3</span>
  <br>Score:
  <span id="score" style="font-weight: bold;">0</span>
  <br>WORD: <span id="display"></span>
  <section id="f1">
    <input type="text" id="demo" onkeydown="if (event.keyCode == 13) go();">
  </section>
</body>
Vicky Gonsalves
  • 11,593
  • 2
  • 37
  • 58