Skip to content

Commit 87a0316

Browse files
committed
2 parents cba54cf + ecf22df commit 87a0316

File tree

8 files changed

+217
-1
lines changed

8 files changed

+217
-1
lines changed

12 - Key Sequence Detection/index-START.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77
</head>
88
<body>
99
<script>
10+
11+
const pressed = [];
12+
const secretCode = 'caden';
13+
14+
window.addEventListener('keyup', (e) => {
15+
console.log(e.key);
16+
pressed.push(e.key);
17+
pressed.splice(-secretCode.length - 1, pressed.length - secretCode.length);
18+
if (pressed.join('').includes(secretCode)) {
19+
cornify_add();
20+
}
21+
console.log(pressed);
22+
});
1023
</script>
1124
</body>
1225
</html>

13 - Slide in on Scroll/index-START.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ <h1>Slide in on Scroll</h1>
5858
};
5959
}
6060

61+
const slideImages = document.querySelectorAll('.slide-in');
62+
63+
function checkSlide(e) {
64+
slideImages.forEach(sliderImage => {
65+
// Halfway through the image
66+
const slideInAt = (window.scrollY + window.innerHeight) - sliderImage.height / 2;
67+
// The bottom of the image
68+
const imageBottom = sliderImage.offsetTop + sliderImage.height;
69+
const isHalfShown = slideInAt > sliderImage.offsetTop;
70+
const isNotScrolledPast = window.scrollY < imageBottom;
71+
if(isHalfShown && isNotScrolledPast) {
72+
sliderImage.classList.add('active');
73+
} else {
74+
sliderImage.classList.remove('active');
75+
}
76+
});
77+
console.log(e);
78+
}
79+
80+
window.addEventListener('scroll', debounce(checkSlide));
81+
6182
</script>
6283

6384
<style>

14 - JavaScript References VS Copying/index-START.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,28 @@
88

99
<script>
1010
// start with strings, numbers and booleans
11+
let age = 100;
12+
let age2 = age;
13+
console.log(age, age2);
14+
age = 200;
15+
console.log(age);
16+
17+
let name = 'Caden';
18+
let name2 = name;
19+
console.log(name, name2);
20+
name = 'Caden Albaugh';
21+
console.log(name, name2);
1122

1223
// Let's say we have an array
1324
const players = ['Wes', 'Sarah', 'Ryan', 'Poppy'];
1425

1526
// and we want to make a copy of it.
27+
const team = players;
28+
29+
console.log(team, players);
1630

1731
// You might think we can just do something like this:
32+
team[3] = 'lux';
1833

1934
// however what happens when we update that array?
2035

@@ -25,27 +40,49 @@
2540
// Why? It's because that is an array reference, not an array copy. They both point to the same array!
2641

2742
// So, how do we fix this? We take a copy instead!
43+
const team2 = players.slice();
2844

2945
// one day
3046

3147
// or create a new array and concat the old one in
48+
const team3 = [].concat(players);
3249

3350
// or use the new ES6 Spread
51+
const team4 = [...players];
52+
53+
const team5 = Array.from(players);
3454

3555
// now when we update it, the original one isn't changed
3656

3757
// The same thing goes for objects, let's say we have a person object
3858

3959
// with Objects
60+
const person = {
61+
name: 'Wes Bos',
62+
age: 80
63+
};
4064

4165
// and think we make a copy:
66+
const captain = person;
67+
captain.number = 99;
4268

4369
// how do we take a copy instead?
70+
const capt2 = Object.assign({}, person, { number: 99 });
4471

4572
// We will hopefully soon see the object ...spread
73+
capt3 = {...person}; // This is shallow, meaning it will only go one level deep
4674

4775
// Things to note - this is only 1 level deep - both for Arrays and Objects. lodash has a cloneDeep method, but you should think twice before using it.
4876

77+
const wes = {
78+
name: 'Wes',
79+
age: 100,
80+
social: {
81+
twitter: '@wesbos',
82+
facebook: 'wesbos.developer'
83+
}
84+
};
85+
4986
</script>
5087

5188
</body>

15 - LocalStorage/index-START.html

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,89 @@ <h2>LOCAL TAPAS</h2>
2323
<input type="text" name="item" placeholder="Item Name" required>
2424
<input type="submit" value="+ Add Item">
2525
</form>
26+
<div class="button-wrapper">
27+
<button class="check-all" type="button">Check All</button>
28+
<button class="uncheck-all" type="button">Uncheck All</button>
29+
<button class="clear-all" type="button">Clear All</button>
30+
</div>
2631
</div>
2732

2833
<script>
2934
const addItems = document.querySelector('.add-items');
3035
const itemsList = document.querySelector('.plates');
31-
const items = [];
36+
const items = JSON.parse(localStorage.getItem('items')) || [];
37+
const checkAllButton = document.querySelector('.check-all');
38+
const uncheckAllButton = document.querySelector('.uncheck-all');
39+
const clearAllButton = document.querySelector('.clear-all');
40+
41+
function addItem(e) {
42+
e.preventDefault();
43+
const text = (this.querySelector('[name="item"]')).value;
44+
const item = {
45+
text, // ES6 shorthand property
46+
done: false
47+
};
48+
49+
items.push(item);
50+
populateList(items, itemsList);
51+
localStorage.setItem('items', JSON.stringify(items));
52+
this.reset();
53+
}
54+
55+
function populateList(plates = [], platesList) {
56+
platesList.innerHTML = plates.map(( plate, i ) => {
57+
return `
58+
<li>
59+
<input type="checkbox" data-index=${i} id="item${i}" ${plate.done ? 'checked' : ''} />
60+
<label for="item${i}">${plate.text}</label>
61+
</li>
62+
`;
63+
}).join('');
64+
}
65+
66+
function toggleDone(e) {
67+
if (!e.target.matches('input')) return; // skip this unless it's an input
68+
const el = e.target;
69+
const index = el.dataset.index;
70+
items[index].done = !items[index].done;
71+
localStorage.setItem('items', JSON.stringify(items));
72+
populateList(items, itemsList);
73+
}
74+
75+
function checkAll() {
76+
var list = JSON.parse(localStorage.getItem('items'));
77+
var checkedList = list.map(item => {
78+
item.done = true;
79+
return item;
80+
});
81+
82+
localStorage.setItem('items', JSON.stringify(checkedList));
83+
populateList(checkedList, itemsList);
84+
}
85+
86+
function uncheckAll() {
87+
var list = JSON.parse(localStorage.getItem('items'));
88+
var checkedList = list.map(item => {
89+
item.done = false;
90+
return item;
91+
});
92+
93+
localStorage.setItem('items', JSON.stringify(checkedList));
94+
populateList(checkedList, itemsList);
95+
}
96+
97+
function clearAll() {
98+
localStorage.removeItem('items');
99+
populateList([], itemsList);
100+
}
101+
102+
addItems.addEventListener('submit', addItem);
103+
itemsList.addEventListener('click', toggleDone);
104+
checkAllButton.addEventListener('click', checkAll);
105+
uncheckAllButton.addEventListener('click', uncheckAll);
106+
clearAllButton.addEventListener('click', clearAll);
107+
108+
populateList(items, itemsList);
32109

33110
</script>
34111

15 - LocalStorage/style.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,17 @@
7676
outline:0;
7777
border:1px solid rgba(0,0,0,0.1);
7878
}
79+
80+
.button-wrapper {
81+
display: flex;
82+
margin: 10px 0;
83+
}
84+
85+
button {
86+
width: calc(33.333% - 20px);
87+
flex: 1;
88+
justify-content: space-between;
89+
padding:10px;
90+
outline:0;
91+
border:1px solid rgba(0,0,0,0.1);
92+
}

16 - Mouse Move Shadow/index-start.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ <h1 contenteditable>🔥WOAH!</h1>
3131
</style>
3232

3333
<script>
34+
const hero = document.querySelector('.hero');
35+
const text = hero.querySelector('h1');
36+
const walk = 100; // 100px
37+
38+
function shadow(e) {
39+
const { offsetWidth: width, offsetHeight: height } = hero;
40+
let { offsetX: x, offsetY: y } = e;
41+
42+
if (this !== e.target) {
43+
x = x + e.target.offsetLeft;
44+
y = y + e.target.offsetTop;
45+
}
46+
47+
const xWalk = Math.round((x / width * walk) - (walk / 2));
48+
const yWalk = Math.round((y / height * walk) - (walk / 2));
49+
50+
text.style.textShadow = `
51+
${xWalk}px ${yWalk}px 0 rgba(255, 0, 255, 0.7),
52+
${xWalk * -1}px ${yWalk}px 0 rgba(0, 255, 255, 0.7),
53+
${yWalk}px ${xWalk * -1}px 0 rgba(255, 255, 0, 0.7),
54+
${yWalk * -1}px ${xWalk}px 0 rgba(70, 170, 110, 0.7)
55+
`;
56+
}
57+
58+
hero.addEventListener('mousemove', shadow);
3459
</script>
3560
</body>
3661
</html>

17 - Sort Without Articles/index-START.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
<script>
4646
const bands = ['The Plot in You', 'The Devil Wears Prada', 'Pierce the Veil', 'Norma Jean', 'The Bled', 'Say Anything', 'The Midway State', 'We Came as Romans', 'Counterparts', 'Oh, Sleeper', 'A Skylit Drive', 'Anywhere But Here', 'An Old Dog'];
4747

48+
function strip(bandNames) {
49+
return bandNames.replace(/^(a |the |an )/i, '').trim();
50+
}
51+
52+
const sortedBands = bands.sort( (a, b) => strip(a) > strip(b) ? 1 : -1 );
53+
54+
document.querySelector('#bands').innerHTML =
55+
sortedBands
56+
.map(band => `<li>${band}</li>`)
57+
.join('');
4858

4959
</script>
5060

18 - Adding Up Times with Reduce/index-START.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,25 @@
182182
</li>
183183

184184
<script>
185+
const timeNodes = Array.from(document.querySelectorAll('[data-time]'));
186+
187+
const seconds = timeNodes
188+
.map(node => node.dataset.time)
189+
.map(timeCode => {
190+
const [mins, secs] = timeCode.split(':')
191+
.map(parseFloat)
192+
return (mins * 60) + secs;
193+
})
194+
.reduce((total, seconds) => total + seconds);
195+
196+
let secondsLeft = seconds;
197+
const hours = Math.floor(seconds / 3600);
198+
secondsLeft = secondsLeft % 3600;
199+
const minutes = Math.floor(secondsLeft / 60);
200+
secondsLeft = secondsLeft % 60;
201+
202+
console.log(hours, minutes, secondsLeft);
203+
185204
</script>
186205
</body>
187206
</html>

0 commit comments

Comments
 (0)