Skip to content

Commit 119830d

Browse files
committed
Lessons 15 - 18
1 parent 2b16111 commit 119830d

4 files changed

Lines changed: 130 additions & 2 deletions

File tree

15 - LocalStorage/index-START.html

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,77 @@ <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 style="margin-top: 20px;">
27+
<button onClick="clearAll()">Clear All</button>
28+
<button onClick="checkAll()">Check All</button>
29+
<button onClick="uncheckAll()">Uncheck 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+
38+
function addItem(e) {
39+
e.preventDefault();
40+
const text = (this.querySelector('[name=item]')).value;
41+
const item = {
42+
text,
43+
done: false
44+
}
45+
46+
items.push(item);
47+
populateList(items, itemsList);
48+
localStorage.setItem('items', JSON.stringify(items)); // can convert back to object using JSON.parse()
49+
this.reset();
50+
}
51+
52+
function populateList(plates = [], platesList) {
53+
platesList.innerHTML = plates.map((plate, i) => {
54+
return `
55+
<li>
56+
<input type="checkbox" data-index=${i} id="item${i}" ${plate.done ? 'checked' : ''} />
57+
<label for="item${i}">${plate.text}</label>
58+
</li>
59+
`;
60+
}).join('');
61+
}
62+
63+
function toggleDone(e) {
64+
if(!e.target.matches('input')) return; // skip this unless it's an input
65+
const el = e.target;
66+
const index = el.dataset.index;
67+
items[index].done = !items[index].done;
68+
localStorage.setItem('items', JSON.stringify(items));
69+
populateList(items,itemsList);
70+
}
71+
72+
function clearAll() {
73+
items.length = 0;
74+
localStorage.removeItem('items');
75+
populateList(items, itemsList);
76+
}
77+
78+
function checkAll() {
79+
items.forEach(item => {
80+
item.done = true;
81+
});
82+
localStorage.setItem('items', JSON.stringify(items));
83+
populateList(items, itemsList);
84+
}
85+
86+
function uncheckAll() {
87+
items.forEach(item => {
88+
item.done = false;
89+
});
90+
localStorage.setItem('items', JSON.stringify(items));
91+
populateList(items, itemsList);
92+
}
93+
94+
addItems.addEventListener('submit', addItem);
95+
itemsList.addEventListener('click', toggleDone);
96+
populateList(items, itemsList);
3297

3398
</script>
3499

16 - Mouse Move Shadow/index-START.html

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,40 @@ <h1 contenteditable>🔥WOAH!</h1>
2929
}
3030

3131
h1 {
32-
text-shadow: 10px 10px 0 rgba(0,0,0,1);
32+
/* text-shadow: 10px 10px 0 rgba(0,0,0,1); */
3333
font-size: 100px;
3434
}
3535
</style>
3636

3737
<script>
38+
const hero = document.querySelector('.hero');
39+
const text = hero.querySelector('h1');
40+
const walk = 100; // 100px
41+
42+
function shadow(e) {
43+
const { offsetWidth: width, offsetHeight: height } = hero;
44+
45+
let { offsetX: x, offsetY: y } = e;
46+
47+
if (this !== e.target) {
48+
x = x + e.target.offsetLeft;
49+
y = y + e.target.offsetTop;
50+
}
51+
52+
const xWalk = (x / width * walk) - (walk / 2);
53+
const yWalk = (y / height * walk) - (walk / 2);
54+
55+
text.style.textShadow = `
56+
${xWalk}px ${yWalk}px 0 rgba(255,0,255,0.7),
57+
${xWalk * -1}px ${yWalk}px 0 rgba(0,255,255,0.7),
58+
${yWalk}px ${xWalk * -1}px 0 rgba(0,255,0,0.7),
59+
${yWalk * -1}px ${xWalk}px 0 rgba(0,0,255,0.7)
60+
`;
61+
62+
// console.log(xWalk, yWalk);
63+
}
64+
65+
hero.addEventListener('mousemove', shadow);
3866
</script>
3967
</body>
4068
</html>

17 - Sort Without Articles/index-START.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,19 @@
4747

4848
<script>
4949
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'];
50+
const ul = document.getElementById('bands');
5051

52+
function strip(bandName) {
53+
return bandName.replace(/^(a |the |an )/i, '').trim();
54+
}
55+
56+
sortedBands = bands.sort((a,b) => strip(a) > strip(b) ? 1 : -1);
57+
58+
sortedBands.map(band => {
59+
let li = document.createElement('li');
60+
li.appendChild(document.createTextNode(band));
61+
ul.appendChild(li);
62+
})
5163

5264
</script>
5365

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,29 @@
182182
</li>
183183
</ul>
184184
<script>
185+
186+
const timeNodes = [...document.querySelectorAll('[data-time]')];
187+
188+
const seconds = timeNodes
189+
.map(node => node.dataset.time)
190+
.map(timeCode => {
191+
const [mins, secs] = timeCode.split(':').map(parseFloat);
192+
return (mins * 60) + secs;
193+
})
194+
.reduce((total, vidSeconds) => total + vidSeconds);
195+
196+
let secondsLeft = seconds;
197+
const hours = Math.floor(secondsLeft / 3600);
198+
secondsLeft = secondsLeft % 3600;
199+
200+
const mins = Math.floor(secondsLeft / 60);
201+
secondsLeft = secondsLeft % 60;
202+
203+
console.log(hours, mins, secondsLeft);
204+
205+
206+
console.log(seconds);
207+
185208
</script>
186209
</body>
187210
</html>

0 commit comments

Comments
 (0)