This is a JavaScript practice with JavaScript30 by Wes Bos without any frameworks, no compilers, no boilerplate, and no libraries.
keyup
, array.push()
, array.join()
, array.includes()
view demo here
Let's take a one more look the .join()
The .join()
methond joins all elements of an array (or an array-like object
) into a string.
-
syntax
If an element is undefined or null, it is converted to the empty string.
array.join() array.join(seperator)
seperator
(optional): Specifies a string to separate each element of the array. Defaults to,
.
-
example
var a = ['Wind', 'Rain', 'Fire']; a.join(); // 'Wind,Rain,Fire' a.join(', '); // 'Wind, Rain, Fire' a.join(' + '); // 'Wind + Rain + Fire' a.join(''); // 'WindRainFire' a.join(' '); // 'Wind Rain Fire'
- we can
console.log()
thee.key
out to see the name of key we pressed
console.log(e.key);
- then use
.push()
to combine key names into an array
pressed.push(e.key);
- it starts to push out the first one item in the array, if length is over the budget of
secretCode.length
letters
pressed.splice(- secretCode.length - 1, pressed.length - secretCode.length);
-
check the array to see if pressed keys matches the
secretCode
, and then add cornify effect if matched -
.join()
to turn the array into a string
if(pressed.join('').includes(secretCode)) {
console.log('Wow, Amazing!');
cornify_add();
}
- I personally add two new lines for print the
secretCode
out if matched
decode.innerHTML= '';
decode.insertAdjacentHTML('afterbegin', secretCode);