Skip to content

Instantly share code, notes, and snippets.

@daniel-schroeder-dev
Created October 10, 2021 21:10
Show Gist options
  • Select an option

  • Save daniel-schroeder-dev/547c3f678725792a91f7d2c90436a8b2 to your computer and use it in GitHub Desktop.

Select an option

Save daniel-schroeder-dev/547c3f678725792a91f7d2c90436a8b2 to your computer and use it in GitHub Desktop.
Timer
var words = wordsList, time = 30, correctWords = 0;
var answer, timeout, timeInterval;
$('.scoreboard').hide();
timeInterval = setInterval(timer, 1000);
var images = [
'saitama.png',
'hulk.png',
'thanos.png',
'samus.jpg'
]
function nextWord() {
$('#input-word').val("");
var length = words.length;
var num = Math.random() * length;
var randomNum = Math.floor(num);
answer = words[randomNum]
$('#word').text(scramble(words[randomNum]));
words.splice(randomNum, 1);
clearTimeout(timeout);
timeout = setTimeout(timeup, 30000);
}
$('#robot').click(nextWord);
nextWord();
function scramble(word) {
var mixed = "";
var charArray = word.split('');
while (charArray.length != 0) {
var length = charArray.length;
var randomNum = Math.floor(Math.random() * length);
mixed = mixed + charArray[randomNum];
charArray.splice(randomNum, 1);
}
return mixed;
}
function scrambleAgain() {
var text = $('#word').text();
var mixed = scramble(text);
$('#word').text(mixed);
}
$('#bot').click(scrambleAgain);
function checkWord() {
var typed = $('#input-word').val()
if (typed == answer) {
nextWord();
correctWords++;
$('#word-count').text(correctWords);
// if correctWords is divisble by three
// add 5 seconds to the time
} else if (answer.includes(typed)) {
$('#input-word').removeClass('wrong');
} else {
$('#input-word').addClass('wrong');
}
}
function timeup() {
$('.app').hide();
$('.scoreboard').show();
clearInterval(timeInterval);
$('#jcount').text(correctWords);
if (correctWords > 2 && correctWords <= 4) {
$('#s').attr('src', images[0]);
}
else if (correctWords > 5 && correctWords <= 7) {
$('#s').attr('src', images[1]);
}
else if (correctWords > 8 && correctWords <= 10) {
$('#s').attr('src', images[2]);
}
else if (correctWords > 10 && correctWords <= 15) {
$('#s').attr('src', images[3]);
}
}
function timer() {
time--;
// if we're out of time, run timeup()
$('#time').text(`Time left: ${time} secs`);
}
$('#input-word').keyup(checkWord);
function restart() {
$('.scoreboard').hide();
$('#time').show();
$('.app').show();
time = 30;
correctWords = 0;
words = wordsList;
$('#word-count').text(correctWords);
timeInterval = setInterval(timer, 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment