From 300f694e353f8cd62c97ca02e49140c9af1d5039 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Thu, 15 Dec 2016 21:17:08 +0100 Subject: [PATCH 01/30] First lesson --- 01 - JavaScript Drum Kit/index-START.html | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/01 - JavaScript Drum Kit/index-START.html b/01 - JavaScript Drum Kit/index-START.html index 4070d32767..e0eabf9fdf 100644 --- a/01 - JavaScript Drum Kit/index-START.html +++ b/01 - JavaScript Drum Kit/index-START.html @@ -58,7 +58,26 @@ From 6d4b1c053befb031a534b95db2c4ea5f939dbddb Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Thu, 15 Dec 2016 21:53:52 +0100 Subject: [PATCH 02/30] Second lession finished --- 02 - JS + CSS Clock/index-START.html | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/02 - JS + CSS Clock/index-START.html b/02 - JS + CSS Clock/index-START.html index 2712384201..ef983de8ca 100644 --- a/02 - JS + CSS Clock/index-START.html +++ b/02 - JS + CSS Clock/index-START.html @@ -56,6 +56,9 @@ } .hand { + transform-origin: 100%; + transform: rotate(90deg); + transition-duration(0.2s); width:50%; height:6px; background:black; @@ -66,6 +69,20 @@ From c7db5262b5f4ae9b8c5fdfec9712cba635ac5906 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Sat, 17 Dec 2016 12:47:34 +0100 Subject: [PATCH 03/30] 3 done --- 03 - CSS Variables/index-START.html | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/03 - CSS Variables/index-START.html b/03 - CSS Variables/index-START.html index bf0f33e3ba..29abadad57 100644 --- a/03 - CSS Variables/index-START.html +++ b/03 - CSS Variables/index-START.html @@ -22,6 +22,22 @@

Update CSS Variables with JS

From d427e6fd69b5a59a0ce46953419c4fd1a517bdb1 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Sun, 18 Dec 2016 13:58:15 +0100 Subject: [PATCH 04/30] #4 done --- 04 - Array Cardio Day 1/index-START.html | 31 +++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/04 - Array Cardio Day 1/index-START.html b/04 - Array Cardio Day 1/index-START.html index 6e28e357d0..e76624017b 100644 --- a/04 - Array Cardio Day 1/index-START.html +++ b/04 - Array Cardio Day 1/index-START.html @@ -27,28 +27,57 @@ // Array.prototype.filter() // 1. Filter the list of inventors for those who were born in the 1500's + let inventorsOf1500 = inventors.filter((inventor) => (inventor.year < 1600 && inventor.year >= 1500)); + console.table(inventorsOf1500); // Array.prototype.map() // 2. Give us an array of the inventory first and last names + let names = inventors.map((inventor) => inventor.first + ' ' + inventor.last); + console.log(names); // Array.prototype.sort() // 3. Sort the inventors by birthdate, oldest to youngest + let inventorsSortByBirthday = inventors.sort((inventor1, inventor2) => inventor1.year - inventor2.year); + console.table(inventorsSortByBirthday); + // Array.prototype.reduce() // 4. How many years did all the inventors live? + let yearsOfAllInventors = inventors.reduce((i, inventor) => i + (inventor.passed - inventor.year), 0); + console.log(yearsOfAllInventors); - // 5. Sort the inventors by years lived + // 5. Sort the inventors by years lived highest at the beginning + let inventorsByYearsLived = inventors.sort((inventor1, inventor2) => (inventor2.passed - inventor2.year) - (inventor1.passed - inventor1.year)); + console.table(inventorsByYearsLived); // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris + //let category = document.querySelector('.mw-category'); + //let links = [...category.querySelectorAll('a')].map(a => a.textContent); + //let de = links.filter(rue => rue.includes('de')); + //console.log(de); // 7. sort Exercise // Sort the people alphabetically by last name + let alphabetical = people.sort((current, next) => { + let [currentLast, currentFirst] = current.split(', '); + let [nextLast, nextFirst] = next.split(', '); + return currentLast < nextLast ? -1 : 1; + }); + console.log(alphabetical); // 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' ]; + let numberOfAppearance = data.reduce((appearance, item) => { + if(!appearance[item]) { + appearance[item] = 0; + } + appearance[item]++; + return appearance; + }, {}); + console.log(numberOfAppearance); From 4bcf0bdf8c78b43b643312a6f6ed46b32bd57493 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Sun, 18 Dec 2016 15:54:25 +0100 Subject: [PATCH 05/30] Done. --- 05 - Flex Panel Gallery/index-START.html | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/05 - Flex Panel Gallery/index-START.html b/05 - Flex Panel Gallery/index-START.html index e1d643ad5c..996e30ef51 100644 --- a/05 - Flex Panel Gallery/index-START.html +++ b/05 - Flex Panel Gallery/index-START.html @@ -24,6 +24,7 @@ .panels { min-height:100vh; overflow: hidden; + display: flex; } .panel { @@ -41,6 +42,11 @@ font-size: 20px; background-size:cover; background-position:center; + justify-content: center; + align-items: center; + display: flex; + flex: 1; + flex-direction: column; } @@ -54,6 +60,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.open-active > *:first-child { + transform: translateY(0); + } + + .panel > *:last-child { + transform: translateY(100%); + } + .panel.open-active > *:last-child { + transform: translateY(0); } .panel p { @@ -68,6 +92,7 @@ .panel.open { font-size:40px; + flex: 5; } .cta { @@ -108,6 +133,20 @@ From 243a11fe2313740a4d0e84c1fb17c9c2536d2acc Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Thu, 22 Dec 2016 07:57:51 +0100 Subject: [PATCH 06/30] Type Ahead is done --- 06 - Type Ahead/index-START.html | 36 +++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/06 - Type Ahead/index-START.html b/06 - Type Ahead/index-START.html index 1436886918..821d4706d0 100644 --- a/06 - Type Ahead/index-START.html +++ b/06 - Type Ahead/index-START.html @@ -15,7 +15,41 @@ From 8c3cf3ca020ea737ff6f0ecf50c4e8020c22ed95 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Sat, 24 Dec 2016 15:18:43 +0100 Subject: [PATCH 07/30] More array cardio --- 07 - Array Cardio Day 2/index-START.html | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/07 - Array Cardio Day 2/index-START.html b/07 - Array Cardio Day 2/index-START.html index 206ec31aa0..fa94a91ac9 100644 --- a/07 - Array Cardio Day 2/index-START.html +++ b/07 - Array Cardio Day 2/index-START.html @@ -27,14 +27,22 @@ // Some and Every Checks // Array.prototype.some() // is at least one person 19? // Array.prototype.every() // is everyone 19? + let one19 = people.some((person) => (new Date().getFullYear() - person.year) === 19); + let every = people.every(person => (new Date().getFullYear() - person.year) === 19); + console.log(one19); + console.log(every); // 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 + let comment = comments.find((comm) => comm.id === 823423); + console.log(comment); // Array.prototype.findIndex() // Find the comment with this ID // delete the comment with the ID of 823423 + let index = comments.findIndex(comm => comm.id === 823423); + console.log(index); From 6ae01cfeb917a416832adc9b21f7cc26a7ef30e6 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Mon, 26 Dec 2016 19:42:07 +0100 Subject: [PATCH 08/30] Drawed on canvas --- 08 - Fun with HTML5 Canvas/index-START.html | 46 ++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/08 - Fun with HTML5 Canvas/index-START.html b/08 - Fun with HTML5 Canvas/index-START.html index 37c148df07..9ec6c132ac 100644 --- a/08 - Fun with HTML5 Canvas/index-START.html +++ b/08 - Fun with HTML5 Canvas/index-START.html @@ -7,8 +7,52 @@ + const canvas = document.querySelector('#draw'); + const context = canvas.getContext('2d'); + canvas.height = window.innerHeight; + canvas.width = window.innerWidth; + context.strokeStyle = 'BADA55'; + context.lineJoin = 'round'; + context.lineCap = 'round'; + + let lastX = 0; + let lastY = 0; + let isDrawing = false; + let hue = 0; + let direction = true; + + function draw(e) { + if (!isDrawing) return; + console.log(e); + context.strokeStyle = `hsl(${hue}, 100%, 50%)`; + context.beginPath(); + context.moveTo(lastX, lastY); + context.lineTo(e.offsetX, e.offsetY); + context.stroke(); + lastX = e.offsetX; + lastY = e.offsetY; + hue++; + if (context.lineWidth >= 100 || context.lineWidth < 0) { + direction = !direction; + } + if (direction) { + context.lineWidth++; + } else { + context.lineWidth--; + } + } + + window.addEventListener('mousemove', draw); + window.addEventListener('mousedown', (e) => { + isDrawing = true; + lastX = e.offsetX; + lastY = e.offsetY; + }); + window.addEventListener('mouseup', () => isDrawing = false); + window.addEventListener('mouseleave', () => isDrawing = false); + + From 6ae2dbe08772af05ca1883fa44551072f44981b0 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Wed, 4 Jan 2017 20:36:48 +0100 Subject: [PATCH 17/30] Sort the bands --- 17 - Sort Without Articles/index-START.html | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/17 - Sort Without Articles/index-START.html b/17 - Sort Without Articles/index-START.html index cfaf3e0440..0d0e29dde0 100644 --- a/17 - Sort Without Articles/index-START.html +++ b/17 - Sort Without Articles/index-START.html @@ -44,8 +44,17 @@ From 5e03b392956a7826cf091e50d1e9227b2c67ebbf Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Thu, 5 Jan 2017 19:18:02 +0100 Subject: [PATCH 18/30] Accumulate video times --- 18 - Adding Up Times with Reduce/index-START.html | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/18 - Adding Up Times with Reduce/index-START.html b/18 - Adding Up Times with Reduce/index-START.html index 3eaee0f3ef..521516fb55 100644 --- a/18 - Adding Up Times with Reduce/index-START.html +++ b/18 - Adding Up Times with Reduce/index-START.html @@ -182,6 +182,21 @@ From 24ffeaf65b2d9cb632caec2aeb8349cc2a3b3b7d Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Sun, 8 Jan 2017 20:14:21 +0100 Subject: [PATCH 19/30] Change webcam image --- 19 - Webcam Fun/index.html | 14 +++++- 19 - Webcam Fun/scripts.js | 95 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/19 - Webcam Fun/index.html b/19 - Webcam Fun/index.html index d4ffc4dc2a..00e331f632 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..0d28351d75 100644 --- a/19 - Webcam Fun/scripts.js +++ b/19 - Webcam Fun/scripts.js @@ -3,3 +3,98 @@ const canvas = document.querySelector('.photo'); const ctx = canvas.getContext('2d'); const strip = document.querySelector('.strip'); const snap = document.querySelector('.snap'); +const filter = {}; +document.querySelectorAll('.filterSelector input').forEach(input => { + filter[input.id] = input.checked; + input.addEventListener('change', (e) => filter[e.target.id] = e.target.checked); +}); + +function getVideo() { + navigator.mediaDevices.getUserMedia({audio: false, video: true}) + .then(videoStream => { + console.log(videoStream); + video.src = window.URL.createObjectURL(videoStream); + video.play(); + }) + .catch(error => console.log(error)); +} + +function paintPhoto() { + const height = video.videoHeight; + const width = video.videoWidth; + + canvas.height = height; + canvas.width = width; + + return setInterval(() => { + ctx.drawImage(video, 0, 0, width, height); + let pixels = ctx.getImageData(0, 0, width, height); + if (filter.redFilter) { + pixels = redEffect(pixels); + } + if (filter.slowFilter) { + ctx.globalAlpha = 0.1; + } else { + ctx.globalAlpha = 1; + } + if (filter.rgbFilter) { + pixels = rgbSplit(pixels); + } + if (filter.colorFilter) { + pixels = removeColor(pixels); + } + ctx.putImageData(pixels, 0, 0); + }, 16); +} + +function redEffect(pixels) { + for(let i = 0; i < pixels.data.length; i += 4) { + pixels.data[i+0] = pixels.data[i+0] + 100; + pixels.data[i+1] = pixels.data[i+1] - 100; + pixels.data[i+2] = pixels.data[i+2] - 100; + } + return pixels; +} + +function rgbSplit(pixels) { + for(let i = 0; i < pixels.data.length; i += 4) { + pixels.data[i+300] = pixels.data[i+0]; + pixels.data[i-150] = pixels.data[i+1]; + pixels.data[i+180] = pixels.data[i+2]; + } + return pixels; +} + +function removeColor(pixels) { + const levels = {}; + document.querySelectorAll('.rgb input').forEach(input => levels[input.name] = input.value); + + for (let i = 0; i < pixels.data.length; i += 4) { + let red = pixels.data[i]; + let green = pixels.data[i+1]; + let blue = pixels.data[i+2]; + + if (red >= levels.rmin && red <= levels.rmax + && green >= levels.gmin && green <= levels.gmax + && blue >= levels.bmin && blue <= levels.bmax) { + pixels.data[i+3] = 0; + } + } + return pixels; +} + +function takePhoto() { + snap.currentTime = 0; + snap.play(); + + const data = canvas.toDataURL('image/jpeg'); + + const link = document.createElement('a'); + link.href = data; + link.innerHTML = `screenshot`; + link.setAttribute('downloda', 'webcam_picture'); + strip.insertBefore(link, strip.firstChild); +} +getVideo(); + +video.addEventListener('canplay', paintPhoto); From 731711f9fa39226fc1897381252bfce51ad55452 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Tue, 17 Jan 2017 20:38:22 +0100 Subject: [PATCH 20/30] Done without mic --- 20 - Speech Detection/index-START.html | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/20 - Speech Detection/index-START.html b/20 - Speech Detection/index-START.html index d3395cca35..dab4e9c46f 100644 --- a/20 - Speech Detection/index-START.html +++ b/20 - Speech Detection/index-START.html @@ -12,6 +12,26 @@ From 563a6212722eadac310553f5d494c0cebd6ff100 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Tue, 17 Jan 2017 21:08:08 +0100 Subject: [PATCH 21/30] Didn't work on my device --- 21 - Geolocation/index-START.html | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/21 - Geolocation/index-START.html b/21 - Geolocation/index-START.html index a1b981b1cd..7b22c2169a 100644 --- a/21 - Geolocation/index-START.html +++ b/21 - Geolocation/index-START.html @@ -57,6 +57,12 @@

/*Compass: https://thenounproject.com/search/?q=compass&i=592352*/ From e7c1df0c0d4a76144512d309d5cf75dda6cd976e Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Wed, 18 Jan 2017 17:46:25 +0100 Subject: [PATCH 22/30] Synthesis speech --- 23 - Speech Synthesis/index-START.html | 46 ++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/23 - Speech Synthesis/index-START.html b/23 - Speech Synthesis/index-START.html index e890008d36..9e1f004fc3 100644 --- a/23 - Speech Synthesis/index-START.html +++ b/23 - Speech Synthesis/index-START.html @@ -29,12 +29,46 @@

The Voiceinator 5000

From 2c65b9f3d46f372783766119b54ed2b5a275702a Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Thu, 19 Jan 2017 19:25:13 +0100 Subject: [PATCH 23/30] The bubble follows me --- .../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..59812118bc 100644 --- a/22 - Follow Along Link Highlighter/index-START.html +++ b/22 - Follow Along Link Highlighter/index-START.html @@ -27,6 +27,27 @@ From 56f0dd9cfc24983e36ce108d65db9e539e7648e8 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Thu, 19 Jan 2017 21:30:21 +0100 Subject: [PATCH 24/30] Sticky header --- 24 - Sticky Nav/index-START.html | 15 +++++++++++++++ 24 - Sticky Nav/style-START.css | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/24 - Sticky Nav/index-START.html b/24 - Sticky Nav/index-START.html index e7bc67e9a5..61900fa7ad 100644 --- a/24 - Sticky Nav/index-START.html +++ b/24 - Sticky Nav/index-START.html @@ -55,6 +55,21 @@

A story about getting lost.

diff --git a/24 - Sticky Nav/style-START.css b/24 - Sticky Nav/style-START.css index c6d59a31b3..71d8e8b53a 100644 --- a/24 - Sticky Nav/style-START.css +++ b/24 - Sticky Nav/style-START.css @@ -46,6 +46,10 @@ nav { transition:all 0.5s; position: relative; z-index: 1; + +} +.fixed nav { + position: fixed; } nav ul { @@ -72,6 +76,10 @@ li.logo { font-size: 30px; } +.fixed .logo { + max-width: 500px; +} + li.logo a { color:black; } From 43dd9c8649a69ae2a125d076fc7522372e4cbc46 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Sun, 22 Jan 2017 11:13:09 +0100 Subject: [PATCH 25/30] Event handling --- .../index-START.html | 12 ++++++++++++ 1 file changed, 12 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 98f5e070c4..69840f7c96 100644 --- a/25 - Event Capture, Propagation, Bubbling and Once/index-START.html +++ b/25 - Event Capture, Propagation, Bubbling and Once/index-START.html @@ -39,6 +39,18 @@ From 2964ae12736c31b631f46e6e38b5db309d6af005 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Sun, 22 Jan 2017 17:33:57 +0100 Subject: [PATCH 26/30] Fancy dropdown nav --- 26 - Stripe Follow Along Nav/index-START.html | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/26 - Stripe Follow Along Nav/index-START.html b/26 - Stripe Follow Along Nav/index-START.html index 9780d0d1ac..0c870eee08 100644 --- a/26 - Stripe Follow Along Nav/index-START.html +++ b/26 - Stripe Follow Along Nav/index-START.html @@ -208,6 +208,44 @@

Cool

From 610778cec84c9f0b197016ee14c7f38dedd4b4d8 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Wed, 25 Jan 2017 17:32:00 +0100 Subject: [PATCH 27/30] Dragged div --- 27 - Click and Drag/index-START.html | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/27 - Click and Drag/index-START.html b/27 - Click and Drag/index-START.html index b8609315f7..430d89f6d8 100644 --- a/27 - Click and Drag/index-START.html +++ b/27 - Click and Drag/index-START.html @@ -35,6 +35,34 @@ From e80f6b33b38d2c4abdbdc6bd13af93bd872a69d5 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Wed, 1 Feb 2017 18:06:19 +0100 Subject: [PATCH 28/30] Video speed and slow. --- 28 - Video Speed Controller/index-START.html | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/28 - Video Speed Controller/index-START.html b/28 - Video Speed Controller/index-START.html index c4cbd4259a..908576fa9c 100644 --- a/28 - Video Speed Controller/index-START.html +++ b/28 - Video Speed Controller/index-START.html @@ -15,6 +15,21 @@ From ae2046bb0aad4d754580ab0776b8801ef6f07a12 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Sat, 4 Feb 2017 17:57:07 +0100 Subject: [PATCH 29/30] Timer completed --- 29 - Countdown Timer/scripts-START.js | 49 +++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/29 - Countdown Timer/scripts-START.js b/29 - Countdown Timer/scripts-START.js index e69de29bb2..a859c5b4e1 100644 --- a/29 - Countdown Timer/scripts-START.js +++ b/29 - Countdown Timer/scripts-START.js @@ -0,0 +1,49 @@ +let countdown; +const displayTime = document.querySelector('.display__time-left'); +const endTimeDisplay = document.querySelector('.display__end-time'); +const buttons = document.querySelectorAll('.timer__button'); +const form = document.querySelector('#custom'); +const minutesInput = form.querySelector('input'); + +function startCoundDown(seconds) { + clearInterval(countdown); + const start = Date.now(); + const end = start + (seconds * 1000); + displayEndTime(end); + + displayRemainingTime(Math.round(end - start)/1000); + countdown = setInterval(() => { + const now = Date.now(); + const timeRemaining = Math.round((end - now) / 1000); + if (timeRemaining < 0) { + clearInterval(countdown); + return; + } + displayRemainingTime(timeRemaining); + }, 1000); +} + +function displayRemainingTime(seconds) { + const remainingMinutes = Math.floor(seconds / 60); + const remainingSeconds = seconds % 60; + const text = `${remainingMinutes}:${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}`; + displayTime.textContent = text; + document.title = text; +} + +function displayEndTime(end) { + end = new Date(end); + const hours = end.getHours(); + const minutes = end.getMinutes(); + endTimeDisplay.textContent = `Be back at ${hours}:${minutes > 9 ? '' : '0'}${minutes}`; +} + +buttons.forEach(button => button.addEventListener('click', function() { + startCoundDown(this.dataset['time']); +})); + +form.addEventListener('submit', (e) => { + e.preventDefault(); + startCoundDown(minutesInput.value * 60); + minutesInput.reset(); +}); From 0220669d87570788450160459de67c41f20f3069 Mon Sep 17 00:00:00 2001 From: Arne Maier Date: Sat, 4 Feb 2017 19:59:56 +0100 Subject: [PATCH 30/30] Mole game with standard functionality --- 30 - Whack A Mole/index-START.html | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/30 - Whack A Mole/index-START.html b/30 - Whack A Mole/index-START.html index 2014ff458c..801552ab92 100644 --- a/30 - Whack A Mole/index-START.html +++ b/30 - Whack A Mole/index-START.html @@ -36,7 +36,44 @@

Whack-a-mole! 0

const holes = document.querySelectorAll('.hole'); const scoreBoard = document.querySelector('.score'); const moles = document.querySelectorAll('.mole'); + let lastIndex; + let timeUp = true; + + function randomTime(min, max) { + return Math.round(Math.random() * (max - min) + min); + } + function randomHole(holes) { + const index = Math.floor(Math.random() * holes.length); + if (index === lastIndex) { + return randomHole(holes); + } + lastIndex = index; + return holes[index]; + } + + function showMole() { + const hole = randomHole(holes); + hole.classList.add('up'); + setTimeout(() => { + hole.classList.remove('up'); + if (!timeUp) showMole(); + }, randomTime(200, 1000)); + } + + function startGame() { + timeUp = false; + scoreBoard.textContent = 0; + showMole(); + setTimeout(() => timeUp = true); + } + + function cachedMole() { + scoreBoard.textContent = parseInt(scoreBoard.textContent) + 1; + this.parentElement.classList.remove('up'); + } + + moles.forEach(mole => mole.addEventListener('click', cachedMole));