const $button_ = document.querySelector('button');
const $level_ = document.querySelector('#level>span');
const $pie_ = document.querySelector('#pie');
const $$pieces_ = document.querySelectorAll('.piece');
const $record_ = document.querySelector('#record');
let pieces_ = [];
// No taps allow before start the game
$pie_.classList.add('avoid-tap');
/**
* Generate next piece (number)
* 1: green, 2: blue, 3: orange, 4: red
*/
function get_random_piece() { // generate a random number between 4 and 1
return Math.floor(Math.random() * (4)) + 1;
}
/**
* Show the sequence you should follow
*/
function show_sequence() {
let i = 0,
e;
$pie_.classList.add('avoid-tap');
(function blink() {
e = document.getElementById(pieces_[i]);
e.classList.remove('blink'); // remove class
void e.offsetWidth; // "restart" -> triggering reflow
e.classList.add('blink'); // show animation
i++;
if (i < pieces_.length) { // if there are more pieces
setTimeout(blink, 1000); // wait 1s before show next one
} else {
setTimeout(function() { // wait 1s before allow user to tap
$pie_.classList.remove('avoid-tap');
}, 1000);
}
})();
}
/**
* Check sequence
*/
let check_sequence = function() {
let i = 0;
return function(piece) {
if (typeof(piece) === 'undefined') { // reset
i = 0;
} else { // play
if (piece === pieces_[i]) { // success
i++;
// Keep playing
if (i >= pieces_.length) { // we have guessed all
$level_.textContent = pieces_.length; // set level before add
pieces_.push(get_random_piece()); // add one more
show_sequence();
i = 0; // check from the beggining
}
} else {
$level_.textContent = '☹ you failed';
document.getElementById(piece).classList.add('fail');
$pie_.classList.add('avoid-tap');
i = 0;
// Save user level if there is an improvement
if...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.