HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

The ListenHear Game - Listen and type the word

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thelistenhearlistentypegamewordand

Problem

What it does:

Speak a random word chosen from an array and ask the user to type the word in the box for 15 seconds

  • If right, +1pts, reset the timer and go to next word.



  • If wrong: do nothing.



  • After 15 seconds: game over.





body {background-color: black; color: white;}
.center {
width: 50%;
height: 50%;
position: absolute;
top:0;
bottom: 0;
left: 0;
right: 0;

margin: auto;
}
.hidden {
display: none;
}




The ListenHear
Game

PlayExit


There was an error.

var read;
var time = 0;
var i = 0;
var words;
var c;
$.getJSON( "https://gist.githubusercontent.com/khanh2003/ae6c144ed12aa4e6dce98c40163935d1/raw/9073f8a40a39ecbe02a38547f99bca9e3637660c/JSON.json", function( data ) {
words=data;
});
function play() {
$(".start").hide(function() {
$(".hidden").removeClass("hidden");
$("#x")[0].innerHTML = "Listen and fill in the blank the word(s).";
setTimeout(game,2000);
});}
function game() {
read = words[Math.floor(Math.random()*words.length)];
$("#x")[0].innerHTML = "Again please!

Give up";
$("input").focus();
speechSynthesis.speak(new SpeechSynthesisUtterance(read));
time = 0;
c=setInterval(function() {
time=time+0.01;
$(".time").text(Math.floor(15-time));
if(time>15) {giveup();}
}, 10);
}
function check() {

if ($("input")[0].value.toUpperCase() === read.toUpperCase()) {
$("#x")[0].innerHTML = "Correct! Score: "+(++i)+"";
time = 0;
setTimeout(game,2000);
clearInterval(c);
}
}
function giveup() {
$("#x")[0].innerHTML = "Time's Up! Score: "+(i)+"
The word(s) are: "+read+"";
time=0;
setTimeout(play,5000);
}
function exit() {$("body").slideUp(function() {window.close;});}




For those who are complaining the code is broken, the SpeechSynthesisAPI (the code depends on) doesn't have good browser support.

Solution

Your game() calls c=setInterval(…), which is balanced by clearInterval(c) in check(). However, giveup() does not call clearInterval(c). As a result, if you just start the game but do nothing, then it gets ridiculously faster and faster.

Context

StackExchange Code Review Q#147569, answer score: 3

Revisions (0)

No revisions yet.