From c1e8cb9c279b2c973af33da1320ec5dc29376b5f Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Mon, 18 Mar 2019 21:45:28 +0000 Subject: [PATCH 01/30] JavaScript 30 - Day 1 --- 01 - JavaScript Drum Kit/index-START.html | 127 +++++++++++++--------- 1 file changed, 75 insertions(+), 52 deletions(-) diff --git a/01 - JavaScript Drum Kit/index-START.html b/01 - JavaScript Drum Kit/index-START.html index 4070d32767..55e80af721 100644 --- a/01 - JavaScript Drum Kit/index-START.html +++ b/01 - JavaScript Drum Kit/index-START.html @@ -1,66 +1,89 @@ + - - JS Drum Kit - + + JS Drum Kit + + -
-
- A - clap -
-
- S - hihat -
-
- D - kick -
-
- F - openhat -
-
- G - boom -
-
- H - ride -
-
- J - snare +
+
+ A + clap +
+
+ S + hihat +
+
+ D + kick +
+
+ F + openhat +
+
+ G + boom +
+
+ H + ride +
+
+ J + snare +
+
+ K + tom +
+
+ L + tink +
-
- K - tom -
-
- L - tink -
-
- - - - - - - - - + + + + + + + + + + + + function removeTransition(e) { + if (e.propertyName !== 'transform') return; + + this.classList.remove('playing'); + } + + const keys = document.querySelectorAll('.key'); + keys.forEach(key => key.addEventListener('transitionend', removeTransition)); + + xwindow.addEventListener('keydown', playSound); + - + + \ No newline at end of file From 79884113b5f18e8f07c9b645b938e21d7d4cf5f9 Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Tue, 19 Mar 2019 21:25:52 +0000 Subject: [PATCH 02/30] JavaScript 30 - Day 2 --- 02 - JS and CSS Clock/index-START.html | 135 ++++++++++++++----------- 1 file changed, 78 insertions(+), 57 deletions(-) diff --git a/02 - JS and CSS Clock/index-START.html b/02 - JS and CSS Clock/index-START.html index 7cbf5f6ba6..5190a7afae 100644 --- a/02 - JS and CSS Clock/index-START.html +++ b/02 - JS and CSS Clock/index-START.html @@ -1,74 +1,95 @@ + - - JS + CSS Clock + + JS + CSS Clock +
-
-
-
-
-
+
+
+
+
+
- - .clock { - width: 30rem; - height: 30rem; - border: 20px solid white; - border-radius: 50%; - margin: 50px auto; - position: relative; - padding: 2rem; - box-shadow: - 0 0 0 4px rgba(0,0,0,0.1), - inset 0 0 0 3px #EFEFEF, - inset 0 0 10px black, - 0 0 10px rgba(0,0,0,0.2); - } + + setInterval(setDate, 1000); + - + + \ No newline at end of file From 3c9a17438435bfa5a904ce9fe98d636e1c9fbb6c Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Wed, 20 Mar 2019 21:37:11 +0000 Subject: [PATCH 03/30] JavaScript 30 - Day 3 --- 03 - CSS Variables/index-START.html | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/03 - CSS Variables/index-START.html b/03 - CSS Variables/index-START.html index 6b9b539c09..7bd900df54 100644 --- a/03 - CSS Variables/index-START.html +++ b/03 - CSS Variables/index-START.html @@ -21,11 +21,26 @@

Update CSS Variables with JS

From a6e93f447e434bf610950231a734c4d4e4ffa5e7 Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Thu, 21 Mar 2019 21:54:55 +0000 Subject: [PATCH 04/30] JavaScript 30 - Day 4 --- 04 - Array Cardio Day 1/index-START.html | 52 +++++++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/04 - Array Cardio Day 1/index-START.html b/04 - Array Cardio Day 1/index-START.html index eec0ffc31d..9097962f44 100644 --- a/04 - Array Cardio Day 1/index-START.html +++ b/04 - Array Cardio Day 1/index-START.html @@ -31,29 +31,77 @@ // Array.prototype.filter() // 1. Filter the list of inventors for those who were born in the 1500's + const fifteen_l = inventors.filter(function(inventor) { + return inventor.year >= 1500 && inventor.year < 1600 + }); + + const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600)); + + console.table(fifteen); // Array.prototype.map() // 2. Give us an array of the inventors' first and last names + const fullNames = inventors.map(inventor => `${inventor.first} ${inventor.last}`); + + console.log(fullNames); // Array.prototype.sort() // 3. Sort the inventors by birthdate, oldest to youngest + const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1); + + console.table(ordered); // Array.prototype.reduce() // 4. How many years did all the inventors live? + const totalYears = inventors.reduce((total, inventor) => { + return total + (inventor.passed - inventor.year) + }, 0); + + console.log(totalYears); // 5. Sort the inventors by years lived + const oldest = inventors.sort((a, b) => { + const last = a.passed - a.year; + const next = b.passed - b.year; + return last > next ? -1 : 1; + }); + + console.table(oldest); // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris - - + /* + const category = document.querySelector('.mw-category'); + const links = Array.from(category.querySelectorAll('a')); + //const links = [...category.querySelectorAll('a')]; // ES6 spread + const de = links + .map(link => link.textContent) + .filter(streetName => streetName.includes('de')); + */ // 7. sort Exercise // Sort the people alphabetically by last name + const alpha = people.sort((lastOne, nextOne) => { + const [aLast, aFirst] = lastOne.split(', '); + const [bLast, bFirst] = nextOne.split(', '); + return aLast > bLast ? 1 : -1; + }); + + console.log(alpha); // 8. Reduce Exercise // Sum up the instances of each of these const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ]; + const transportation = data.reduce((obj, item) => { + if(!obj[item]) { + obj[item] = 0; + } + obj[item]++; + return obj; + }, {}); // start with empty object + + console.log(transportation); + From 8aeaa1779a5c46d2054bdd4670c2a5923923cc02 Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Sat, 23 Mar 2019 00:01:44 +0000 Subject: [PATCH 05/30] JavaScript 30 - Day 5 --- 05 - Flex Panel Gallery/index-START.html | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/05 - Flex Panel Gallery/index-START.html b/05 - Flex Panel Gallery/index-START.html index 71d1c26f00..246f81ded3 100644 --- a/05 - Flex Panel Gallery/index-START.html +++ b/05 - Flex Panel Gallery/index-START.html @@ -26,9 +26,15 @@ .panels { min-height: 100vh; overflow: hidden; + display: flex; } .panel { + flex: 1; + justify-content: center; + align-items: center; + display: flex; + flex-direction: column; background: #6B0F9C; box-shadow: inset 0 0 0 5px rgba(255,255,255,0.1); color: white; @@ -56,6 +62,24 @@ margin: 0; width: 100%; transition: transform 0.5s; + flex: 1 0 auto; + display: flex; + justify-content: center; + align-items: center; + } + + .panel > *:first-child { + transform: translateY(-100%); + } + .panel > *:last-child { + transform: translateY(100%); + } + + .panel.open-active > *:first-child { + transform: translateY(0); + } + .panel.open-active > *:last-child { + transform: translateY(0); } .panel p { @@ -71,6 +95,7 @@ .panel.open { font-size: 40px; + flex: 5; } @@ -105,7 +130,19 @@
From b6c22a3e3e3007d6a7748fb4c385ea11167b332e Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Mon, 25 Mar 2019 21:13:31 +0000 Subject: [PATCH 06/30] JavaScript 30 - Day 6 --- 06 - Type Ahead/index-START.html | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/06 - Type Ahead/index-START.html b/06 - Type Ahead/index-START.html index 109c90fb36..398247063b 100644 --- a/06 - Type Ahead/index-START.html +++ b/06 - Type Ahead/index-START.html @@ -16,6 +16,42 @@ From d457ec48b6f8465b6bfe6cc1588b88f19f65fc72 Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Tue, 26 Mar 2019 21:16:13 +0000 Subject: [PATCH 07/30] JavaScript 30 - Day 7 --- 07 - Array Cardio Day 2/index-START.html | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/07 - Array Cardio Day 2/index-START.html b/07 - Array Cardio Day 2/index-START.html index 969566ff78..5a697b6e83 100644 --- a/07 - Array Cardio Day 2/index-START.html +++ b/07 - Array Cardio Day 2/index-START.html @@ -26,15 +26,42 @@ // Some and Every Checks // Array.prototype.some() // is at least one person 19 or older? + // const isAdult = people.some(person => { + // const currentYear = (new Date()).getFullYear(); + // return currentYear - person.year >= 19; + // }); + const isAdult = people.some(person => ((new Date()).getFullYear()) - person.year >= 19); + + console.log({isAdult}); + // Array.prototype.every() // is everyone 19 or older? + const allAdults = people.every(person => ((new Date()).getFullYear()) - person.year >= 19); + + console.log({allAdults}); // Array.prototype.find() // Find is like filter, but instead returns just the one you are looking for // find the comment with the ID of 823423 + const comment = comments.find(comment => (comment.id === 823423)); + + console.log(comment); + // Array.prototype.findIndex() // Find the comment with this ID // delete the comment with the ID of 823423 + const index = comments.findIndex(comment => (comment.id === 823423)); + + // comments.splice(index, 1); + // console.table(comments); + + const newComments = [ + ...comments.slice(0, index), + ...comments.slice(index + 1) + ]; + + console.table(newComments); + From 99d2d8ef6d6ee8b12c3dc1c6ad2346674f64f079 Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Wed, 27 Mar 2019 22:34:05 +0000 Subject: [PATCH 08/30] JavaScript 30 - Day 8 --- 08 - Fun with HTML5 Canvas/index-START.html | 52 +++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/08 - Fun with HTML5 Canvas/index-START.html b/08 - Fun with HTML5 Canvas/index-START.html index 9da9b5b3c5..11e968ae28 100644 --- a/08 - Fun with HTML5 Canvas/index-START.html +++ b/08 - Fun with HTML5 Canvas/index-START.html @@ -7,6 +7,58 @@ From fbcee51d414b82ee5109ee7a855f752cb4814fb3 Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Wed, 10 Apr 2019 22:27:53 +0100 Subject: [PATCH 17/30] JavaScript 30 - Day 17 --- 17 - Sort Without Articles/index-START.html | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/17 - Sort Without Articles/index-START.html b/17 - Sort Without Articles/index-START.html index 2b6c9546e9..f09a45c161 100644 --- a/17 - Sort Without Articles/index-START.html +++ b/17 - Sort Without Articles/index-START.html @@ -48,7 +48,15 @@ From 23a293a705e50b25a42da919d8afc792459a7e9e Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Wed, 10 Apr 2019 22:41:59 +0100 Subject: [PATCH 18/30] JavaScript 30 - Day 18 --- .../index-START.html | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/18 - Adding Up Times with Reduce/index-START.html b/18 - Adding Up Times with Reduce/index-START.html index abdf4c91af..9094250aaf 100644 --- a/18 - Adding Up Times with Reduce/index-START.html +++ b/18 - Adding Up Times with Reduce/index-START.html @@ -182,6 +182,24 @@ From 1e4402770ff3025cdeb18937762f2d7f1eced3d3 Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Thu, 11 Apr 2019 23:17:05 +0100 Subject: [PATCH 19/30] JavaScript 30 - Day 19 --- 19 - Webcam Fun/index.html | 4 +- 19 - Webcam Fun/scripts.js | 94 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/19 - Webcam Fun/index.html b/19 - Webcam Fun/index.html index d4ffc4dc2a..cde168020f 100755 --- a/19 - Webcam Fun/index.html +++ b/19 - Webcam Fun/index.html @@ -10,7 +10,7 @@
- +
diff --git a/19 - Webcam Fun/scripts.js b/19 - Webcam Fun/scripts.js index 00355f5a9c..8e764dc426 100644 --- a/19 - Webcam Fun/scripts.js +++ b/19 - Webcam Fun/scripts.js @@ -3,3 +3,97 @@ const canvas = document.querySelector('.photo'); const ctx = canvas.getContext('2d'); const strip = document.querySelector('.strip'); const snap = document.querySelector('.snap'); + +function getVideo() { + navigator.mediaDevices.getUserMedia({ video: true, audio: false }) + .then(localMediaStream => { + console.log(localMediaStream); + video.srcObject = localMediaStream; + video.play(); + }) + .catch(err => { + console.error(`OH NO!!!`, err, err.name + ": " + err.message); + }); +} + +function paintToCanvas() { + const width = video.videoWidth; + const height = video.videoHeight; + + canvas.width = width; + canvas.height = height; + + return setInterval(() => { + ctx.drawImage(video, 0, 0, width, height); + // take the pixels out + let pixels = ctx.getImageData(0, 0, width, height); + // mess with them + // 1. pixels = redEffect(pixels); + // 2. pixels = rgbSplit(pixels); + // ctx.globalAplha = 0.1; + pixels = greenScreen(pixels); + // put them back + ctx.putImageData(pixels, 0, 0); + }, 16) +} + +function takePhoto() { + snap.currentTime = 0; + snap.play(); + + const data = canvas.toDataURL('image/jpeg'); + const link = document.createElement('a'); + link.href = data; + link.setAttribute('download', 'handsome'); + link.innerHTML = `Handsome man`; + strip.insertBefore(link, strip.firstChild); +} + +function redEffect(pixels) { + for (let i = 0; i < pixels.data.length; i+=4) { + pixels.data[i + 0] = pixels.data[i + 0] + 100; // red + pixels.data[i + 1] = pixels.data[i + 1] - 50; // green + pixels.data[i + 2] = pixels.data[i + 2] * 0.5; // blue + } + return pixels; +} + +function rgbSplit(pixels) { + for (let i = 0; i < pixels.data.length; i+=4) { + pixels.data[i - 150] = pixels.data[i + 0]; // red + pixels.data[i + 500] = pixels.data[i + 1]; // green + pixels.data[i - 550] = pixels.data[i + 2]; // blue + } + return pixels; +} + +function greenScreen(pixels) { + const levels = {}; + + document.querySelectorAll('.rgb input').forEach((input) => { + levels[input.name] = input.value; + }); + + for (i = 0; i < pixels.data.length; i = i + 4) { + red = pixels.data[i + 0]; + green = pixels.data[i + 1]; + blue = pixels.data[i + 2]; + alpha = pixels.data[i + 3]; + + if (red >= levels.rmin + && green >= levels.gmin + && blue >= levels.bmin + && red <= levels.rmax + && green <= levels.gmax + && blue <= levels.bmax) { + // take it out! + pixels.data[i + 3] = 0; + } + } + + return pixels; +} + +getVideo(); + +video.addEventListener('canplay', paintToCanvas); \ No newline at end of file From caa3f907f1900274e42acf310af64434cdd4e3b3 Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Fri, 12 Apr 2019 23:01:02 +0100 Subject: [PATCH 20/30] JavaScript 30 - Day 20 --- 20 - Speech Detection/index-START.html | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/20 - Speech Detection/index-START.html b/20 - Speech Detection/index-START.html index 31b4042563..5698b43160 100644 --- a/20 - Speech Detection/index-START.html +++ b/20 - Speech Detection/index-START.html @@ -12,7 +12,28 @@ From 1d1c9f5b5ceed2a4e37384805f0e1355818705c7 Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Mon, 15 Apr 2019 22:35:29 +0100 Subject: [PATCH 21/30] JavaScript 30 - Day 21 --- 21 - Geolocation/index-START.html | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/21 - Geolocation/index-START.html b/21 - Geolocation/index-START.html index 6d48c7a6d1..62a02b03ea 100644 --- a/21 - Geolocation/index-START.html +++ b/21 - Geolocation/index-START.html @@ -59,6 +59,17 @@

/*Compass: https://thenounproject.com/search/?q=compass&i=592352*/ From 245c999aa0de34c228ec5f4ec34303820d7c7474 Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Tue, 16 Apr 2019 22:22:49 +0100 Subject: [PATCH 22/30] JavaScript 30 - Day 22 --- .../index-START.html | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/22 - Follow Along Link Highlighter/index-START.html b/22 - Follow Along Link Highlighter/index-START.html index 8476112b5e..1dc18fb441 100644 --- a/22 - Follow Along Link Highlighter/index-START.html +++ b/22 - Follow Along Link Highlighter/index-START.html @@ -27,6 +27,27 @@ From e58af0e82dc6e41c57e1ea63017ea54a434a520d Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Thu, 18 Apr 2019 22:49:41 +0100 Subject: [PATCH 23/30] JavaScript 30 - Day 23 --- 23 - Speech Synthesis/index-START.html | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/23 - Speech Synthesis/index-START.html b/23 - Speech Synthesis/index-START.html index e890008d36..8f839ed3fd 100644 --- a/23 - Speech Synthesis/index-START.html +++ b/23 - Speech Synthesis/index-START.html @@ -35,6 +35,41 @@

The Voiceinator 5000

const options = document.querySelectorAll('[type="range"], [name="text"]'); const speakButton = document.querySelector('#speak'); const stopButton = document.querySelector('#stop'); + + msg.text = document.querySelector('[name="text"]').value; + + function populateVoices() { + voices = this.getVoices(); + + voicesDropdown.innerHTML = voices + .filter(voice => voice.lang.includes('en')) + .map(voice => ``) + .join(''); + } + + function setVoice() { + msg.voice = voices.find(voice => voice.name === this.value); + toggle(); + } + + function toggle(startOver = true) { + speechSynthesis.cancel(); + if (startOver) { + speechSynthesis.speak(msg); + } + } + + function setOption() { + msg[this.name] = this.value; + toggle(); + } + + speechSynthesis.addEventListener('voiceschanged', populateVoices); + voicesDropdown.addEventListener('change', setVoice); + options.forEach(option => option.addEventListener('change', setOption)); + speakButton.addEventListener('click', toggle); + stopButton.addEventListener('click', () => toggle(false)); + //stopButton.addEventListener('click', toggle.bind(null, false)); From fa7611166f295d2d619c19b9cf777337d649a0fd Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Mon, 22 Apr 2019 22:46:13 +0100 Subject: [PATCH 24/30] JavaScript 30 - Day 24 --- 24 - Sticky Nav/index-START.html | 14 +++++++++++++- 24 - Sticky Nav/style-START.css | 13 +++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/24 - Sticky Nav/index-START.html b/24 - Sticky Nav/index-START.html index e7bc67e9a5..b0f3911889 100644 --- a/24 - Sticky Nav/index-START.html +++ b/24 - Sticky Nav/index-START.html @@ -54,7 +54,19 @@

A story about getting lost.

diff --git a/24 - Sticky Nav/style-START.css b/24 - Sticky Nav/style-START.css index b83b9c01ae..5b27cb2832 100644 --- a/24 - Sticky Nav/style-START.css +++ b/24 - Sticky Nav/style-START.css @@ -25,6 +25,10 @@ body { transition: transform 0.5s; } +.fixed-nav .site-wrap { + transform: scale(1); +} + header { text-align: center; height: 50vh; @@ -50,6 +54,11 @@ nav { z-index: 1; } +.fixed-nav nav { + position: fixed; + box-shadow: 0 5px rbga(0, 0, 0, 0.1); +} + nav ul { margin: 0; padding:0; @@ -74,6 +83,10 @@ li.logo { font-size: 30px; } +.fixed-nav li.logo { + max-width: 500px; +} + li.logo a { color: black; } From 53dc07c0c610c633aaac0cdd778c75e3b7b0d0a7 Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Mon, 22 Apr 2019 22:57:21 +0100 Subject: [PATCH 25/30] JavaScript 30 - Day 25 --- .../index-START.html | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/25 - Event Capture, Propagation, Bubbling and Once/index-START.html b/25 - Event Capture, Propagation, Bubbling and Once/index-START.html index 7bd5931e01..207c040a44 100644 --- a/25 - Event Capture, Propagation, Bubbling and Once/index-START.html +++ b/25 - Event Capture, Propagation, Bubbling and Once/index-START.html @@ -42,7 +42,17 @@ From 1493ddd75f907324bacd875c4745b1f533c0ee8f Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Wed, 24 Apr 2019 22:12:21 +0100 Subject: [PATCH 26/30] JavaScript 30 - Day 26 --- 26 - Stripe Follow Along Nav/index-START.html | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/26 - Stripe Follow Along Nav/index-START.html b/26 - Stripe Follow Along Nav/index-START.html index 608d0a6716..6d77f7c22a 100644 --- a/26 - Stripe Follow Along Nav/index-START.html +++ b/26 - Stripe Follow Along Nav/index-START.html @@ -218,8 +218,40 @@

Cool

.button[href*=facebook] { background: #3B5998; } .button[href*=courses] { background: #ffc600; } - From 7a862bad67c202bae2fc476937a219e8bdd44336 Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Wed, 24 Apr 2019 22:26:46 +0100 Subject: [PATCH 27/30] JavaScript 30 - Day 27 --- 27 - Click and Drag/index-START.html | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/27 - Click and Drag/index-START.html b/27 - Click and Drag/index-START.html index b8609315f7..c47549ac6f 100644 --- a/27 - Click and Drag/index-START.html +++ b/27 - Click and Drag/index-START.html @@ -35,6 +35,32 @@ From da28f170d4df5924bf70737b9160c695456f2eb7 Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Thu, 25 Apr 2019 22:56:45 +0100 Subject: [PATCH 28/30] JavaScript 30 - Day 28 --- 28 - Video Speed Controller/index-START.html | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/28 - Video Speed Controller/index-START.html b/28 - Video Speed Controller/index-START.html index 8bd536b18b..b6e0813c5d 100644 --- a/28 - Video Speed Controller/index-START.html +++ b/28 - Video Speed Controller/index-START.html @@ -15,6 +15,23 @@ From 1d113cedd9c2fc633ff5da807382dd48593aefd8 Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Fri, 26 Apr 2019 22:41:33 +0100 Subject: [PATCH 29/30] JavaScript 30 - Day 29 --- 29 - Countdown Timer/scripts-START.js | 50 +++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/29 - Countdown Timer/scripts-START.js b/29 - Countdown Timer/scripts-START.js index e69de29bb2..bbec314d6a 100644 --- a/29 - Countdown Timer/scripts-START.js +++ b/29 - Countdown Timer/scripts-START.js @@ -0,0 +1,50 @@ +let countdown; +const timerDisplay = document.querySelector('.display__time-left'); +const endTime = document.querySelector('.display__end-time'); +const buttons = document.querySelectorAll('[data-time]'); + +function timer(seconds) { + clearInterval(countdown); + const now = Date.now(); + const then = now + seconds * 1000; + displayEndTime(then); + displayTimeLeft(seconds); + + countdown = setInterval(() => { + const secondsLeft = Math.round((then - Date.now()) / 1000); + if (secondsLeft < 0) { + clearInterval(countdown); + return; + } + displayTimeLeft(secondsLeft); + }, 1000); +} + +function displayTimeLeft(seconds) { + const minutes = Math.floor(seconds / 60); + const remainderSeconds = seconds % 60; + const display = `${minutes}:${remainderSeconds < 10 ? '0' : ''}${remainderSeconds}`; + + timerDisplay.textContent = display; + document.title = display; +} + +function displayEndTime(timestamp) { + const end = new Date(timestamp); + const hours = end.getHours(); + const minutes = end.getMinutes(); + endTime.textContent = `Be back at ${hours > 12 ? hours - 12 : hour}:${minutes < 10 ? '0' : ''}${minutes}`; +} + +function startTimer() { + const seconds = parseInt(this.dataset.time); + timer(seconds); +} + +buttons.forEach(button => button.addEventListener('click', startTimer)); +document.customForm.addEventListener('submit', function(e) { + e.preventDefault(); + const mins = parseInt(this.minutes.value); + timer(mins * 60); + this.reset(); +}); \ No newline at end of file From 25989d1d8d1ca9f4079d2712f7cbb348e591fb2f Mon Sep 17 00:00:00 2001 From: Michal Slepko Date: Wed, 1 May 2019 22:45:00 +0100 Subject: [PATCH 30/30] JavaScript 30 - Day 30 --- 30 - Whack A Mole/index-START.html | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/30 - Whack A Mole/index-START.html b/30 - Whack A Mole/index-START.html index 2014ff458c..64c12a6d6f 100644 --- a/30 - Whack A Mole/index-START.html +++ b/30 - Whack A Mole/index-START.html @@ -36,6 +36,56 @@

Whack-a-mole! 0

const holes = document.querySelectorAll('.hole'); const scoreBoard = document.querySelector('.score'); const moles = document.querySelectorAll('.mole'); + let lastHole; + let timeUp = false; + let score = 0; + + function randomTime(min, max) { + return Math.round(Math.random() * (max - min) + min); + } + + function randomHole(holes) { + const idx = Math.floor(Math.random() * holes.length); + const hole = holes[idx]; + if (hole === lastHole) { + return randomHole(holes); + } + + lastHole = hole; + return hole; + } + + function peep() { + const time = randomTime(200, 1000); + const hole = randomHole(holes); + hole.classList.add('up'); + setTimeout(() => { + hole.classList.remove('up'); + if (!timeUp) { + peep(); + } + }, time) + } + + function startGame() { + scoreBoard.textContent = 0; + timeUp = false; + score = 0; + peep(); + setTimeout(() => timeUp = true, 10000); + } + + function bonk(e) { + if (!e.isTrusted) { + console.log('Do not cheat'); + return; + } + score++; + this.classList.remove('up'); + scoreBoard.textContent = score; + } + + moles.forEach(mole => mole.addEventListener('click', bonk));