Skip to content

Commit 79e7a22

Browse files
committed
exercises 8 + 10
1 parent 7b21e0b commit 79e7a22

File tree

2 files changed

+49
-29
lines changed

2 files changed

+49
-29
lines changed

exercises/08 - Fun with HTML5 Canvas/index.html

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030

3131
// Declare & define variable reference for current & last mouse location
3232
const mouse = { x: 0, y: 0 },
33-
last_mouse = { ...mouse};
33+
last_mouse = { ...mouse};
3434

3535
// Declare & define variables to change line hue & line width
3636
let hue = 0,
37-
increaseLineWidth = true;
37+
increaseLineWidth = true;
3838

3939
// mousedown event handler function
4040
const quickDraw = () => {
@@ -45,9 +45,11 @@
4545
ctx.closePath(); // Create path starting from moveTo and ending at lineTo
4646
ctx.stroke(); // Draw the defined path on the canvas
4747

48-
hue >= 360 ? hue = 0 : hue++; // Ternary operator to reset/increase hue value
48+
// Ternary operator to reset/increase hue value
49+
hue >= 360 ? 0 : hue++;
4950

50-
increaseLineWidth ? ctx.lineWidth++ : ctx.lineWidth--; // Ternary operator to increase/decrease line width
51+
// Ternary operator to increase/decrease line width
52+
increaseLineWidth ? ctx.lineWidth++ : ctx.lineWidth--;
5153

5254
// Width of line increases or decreases, staying within a value of 1-100.
5355
if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) increaseLineWidth = !increaseLineWidth;
@@ -59,15 +61,24 @@
5961
[last_mouse.x, last_mouse.y, mouse.x, mouse.y] = [mouse.x, mouse.y, e.offsetX, e.offsetY];
6062
}
6163

64+
// An object with key/value pairs corresponding with the name of the action
65+
// and the function we want to for that action
6266
const quick = {
6367
add: () => myCanvas.addEventListener('mousemove', quickDraw),
6468
rem: () => myCanvas.removeEventListener('mousemove', quickDraw)
6569
}
70+
71+
// Array of event + eventHandler objects to iterate over and use
72+
// as arguments for adding event listeners to myCanvas
73+
const evs = [
74+
{e: 'mousemove', h: setMouseLocation},
75+
{e: 'mousedown', h: quick.add},
76+
{e: 'mouseup', h: quick.rem},
77+
{e: 'mouseout', h: quick.rem}
78+
];
79+
6680
// Attach event listeners
67-
myCanvas.addEventListener('mousemove', setMouseLocation);
68-
myCanvas.addEventListener('mousedown', quick.add);
69-
myCanvas.addEventListener('mouseup', quick.rem);
70-
myCanvas.addEventListener('mouseout', quick.rem);
81+
evs.forEach(({e, h}) => { myCanvas.addEventListener(e, h); });
7182
})(); // Immediately invoke anonymous function
7283

7384
</script>

exercises/10 - Hold Shift and Check Checkboxes/index.html

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
<!--
6161
The following is a common layout you would see in an email client.
6262
63-
When a user clicks a checkbox, holds Shift, and then clicks another checkbox a
63+
When a user clicks a checkbox, holds Shift, and then clicks another checkbox a
6464
few rows down, all the checkboxes inbetween those two checkboxes should be checked.
6565
-->
6666
<div class="inbox">
@@ -105,43 +105,52 @@
105105
<script>
106106
// Wrap JS code in an IIFE, creating a closure so as to not pollute the global namespace
107107
(() => {
108+
109+
// Declare variable that will be defined later as the input that the user
110+
// most recently selected
111+
let lastSelected;
112+
108113
// Declare & define constant as a reference to all inputs of type checkbox
109114
// that are children of the 'inbox' class
110115
const checkBoxes = document.querySelectorAll('.inbox input[type="checkbox"]');
111-
// Declare variable that will be defined later
112-
let lastSelected;
113116

114117
// Event Handler function
115-
function multiCheck(e) {
118+
const multiCheck = (el, e) => {
116119

117120
// If the lastSelected variable is defined and the shift key was pressed
118121
// when this event triggered...
119122
if (lastSelected && e.shiftKey) {
120-
// Set checked property of function context to be the same as the
123+
124+
// Set checked property of checkbox to be the same as the
121125
// checked property of the last selected input
122-
this.checked = lastSelected.checked;
123-
// Declare two variables
124-
let startIdx, endIdx;
125-
// If the ID of the function context is greater than the ID of the last selected input...
126-
this.id > lastSelected.id ?
127-
// ...define variable startIdx as last selected input ID and endIdx as function context ID
128-
[startIdx, endIdx] = [lastSelected.id, this.id] :
126+
el.checked = lastSelected.checked;
127+
128+
// If the ID of the checkbox is greater than the ID of the last selected input...
129+
const [startIdx, endIdx] = el.id > lastSelected.id ?
130+
// ...define variable startIdx as last selected input ID and endIdx as checkbox ID
131+
[lastSelected.id, el.id] :
129132
// ...if not, switch variable definitions
130-
[startIdx, endIdx] = [this.id, lastSelected.id];
133+
[el.id, lastSelected.id];
131134

132135
// Declare & define constant as an array of inputs between the last selected input
133-
// and the function context input
134-
const middleInputs = Array.from(checkBoxes).slice(parseInt(startIdx) + 1, endIdx);
135-
// Set checked property of each input to match checked property of function context input
136-
middleInputs.forEach(checkbox => checkbox.checked = this.checked);
137-
} // If no lastSelected or shiftKey was not pressed, entire if statement is skipped
136+
// and the checkbox input
137+
const middleInputs = [...checkBoxes].slice(parseInt(startIdx) + 1, endIdx);
138+
139+
// Set checked property of each input to match checked property of checkbox input
140+
middleInputs.forEach(checkbox => checkbox.checked = el.checked);
141+
}
142+
143+
// If no lastSelected or shiftKey was not pressed, entire if statement is skipped
138144
// Define lastSelected as the input that was the checked which triggered the event handler
139-
lastSelected = this;
145+
lastSelected = el;
140146
}
147+
141148
// Add event listener to each input that will call multiCheck function on the 'click' event
142-
checkBoxes.forEach(checkBox => checkBox.addEventListener('click', multiCheck));
149+
checkBoxes.forEach(checkBox => {
150+
checkBox.addEventListener('click', multiCheck.bind(null, checkBox));
151+
});
143152
})();
144153
</script>
145154
</body>
146155

147-
</html>
156+
</html>

0 commit comments

Comments
 (0)