Skip to content

Commit 4c4de88

Browse files
authored
Merge pull request #5 from LHJE/day_7
The last half!
2 parents a0ba2a1 + 073566a commit 4c4de88

File tree

20 files changed

+4087
-16
lines changed

20 files changed

+4087
-16
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,27 @@
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, age2);
16+
17+
// let name = 'Wes';
18+
// let name2 = name;
19+
// console.log(name, name2);
20+
// name = 'wesley';
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;
1628

29+
console.log(players, team);
1730
// You might think we can just do something like this:
31+
// team[3] = 'Lux';
1832

1933
// however what happens when we update that array?
2034

@@ -25,12 +39,19 @@
2539
// Why? It's because that is an array reference, not an array copy. They both point to the same array!
2640

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

2944
// one way
3045

3146
// or create a new array and concat the old one in
47+
const team3 = [].concat(players);
3248

3349
// or use the new ES6 Spread
50+
const team4 = [...players];
51+
team4[3] = 'heeee hawww';
52+
console.log(team4);
53+
54+
const team5 = Array.from(players);
3455

3556
// now when we update it, the original one isn't changed
3657

@@ -43,13 +64,35 @@
4364
};
4465

4566
// and think we make a copy:
67+
// const captain = person;
68+
// captain.number = 99;
4669

4770
// how do we take a copy instead?
71+
const cap2 = Object.assign({}, person, { number: 99, age: 12 });
72+
console.log(cap2);
4873

4974
// We will hopefully soon see the object ...spread
75+
const cap3 = {...person};
5076

5177
// 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.
5278

79+
const wes = {
80+
name: 'Wes',
81+
age: 100,
82+
social: {
83+
twitter: '@wesbos',
84+
facebook: 'wesbos.developer'
85+
}
86+
};
87+
88+
console.clear();
89+
console.log(wes);
90+
91+
const dev = Object.assign({}, wes);
92+
93+
const dev2 = JSON.parse(JSON.stringify(wes));
94+
95+
5396
</script>
5497

5598
</body>

15 - LocalStorage/index-START.html

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,53 @@ <h2>LOCAL TAPAS</h2>
2828
<script>
2929
const addItems = document.querySelector('.add-items');
3030
const itemsList = document.querySelector('.plates');
31-
const items = [];
31+
const items = JSON.parse(localStorage.getItem('items')) || [];
32+
33+
function addItem(e) {
34+
35+
e.preventDefault()
36+
37+
const text = (this.querySelector('[name=item]')).value;
38+
const item = {
39+
text,
40+
done: false
41+
};
42+
43+
items.push(item);
44+
populateList(items, itemsList);
45+
localStorage.setItem('items', JSON.stringify(items));
46+
this.reset();
47+
}
48+
49+
function populateList(plates = [], platesList) {
50+
platesList.innerHTML = plates.map((plate, i) => {
51+
return `
52+
<li>
53+
<input type="checkbox" data-index=${i} id="item${i}" ${plate.done ? 'checked' : ''} />
54+
<label for="item${i}">${plate.text}</label>
55+
</li>
56+
`;
57+
}).join('');
58+
}
59+
60+
function toggleDone(e) {
61+
if (!e.target.matches('input')) return; // skip this unless it's an input
62+
const el = e.target;
63+
const index = el.dataset.index;
64+
items[index].done = !items[index].done;
65+
localStorage.setItem('items', JSON.stringify(items));
66+
populateList(items, itemsList);
67+
}
68+
69+
addItems.addEventListener('submit', addItem);
70+
71+
itemsList.addEventListener('click', toggleDone);
72+
73+
populateList(items, itemsList);
74+
3275

3376
</script>
3477

3578

3679
</body>
3780
</html>
38-

16 - Mouse Move Shadow/index-START.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,35 @@ <h1 contenteditable>🔥WOAH!</h1>
3535
</style>
3636

3737
<script>
38+
39+
const hero = document.querySelector('.hero');
40+
const text = hero.querySelector('h1');
41+
const walk = 900;
42+
43+
function shadow(e) {
44+
const { offsetWidth: width, offsetHeight: height } = hero;
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 = Math.round((x / width * walk) - (walk / 2));
53+
const yWalk = Math.round((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+
}
63+
64+
hero.addEventListener('mousemove', shadow);
65+
66+
3867
</script>
3968
</body>
4069
</html>

17 - Sort Without Articles/index-START.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
padding: 0;
2727
box-shadow: 0 0 0 20px rgba(0, 0, 0, 0.05);
2828
}
29-
29+
3030
#bands li {
3131
border-bottom: 1px solid #efefef;
3232
padding: 20px;
3333
}
34-
34+
3535
#bands li:last-child {
3636
border-bottom: 0;
3737
}
@@ -48,6 +48,16 @@
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'];
5050

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

5262
</script>
5363

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,26 @@
182182
</li>
183183
</ul>
184184
<script>
185+
186+
const timeNodes = Array.from(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+
185205
</script>
186206
</body>
187207
</html>

19 - Webcam Fun/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div class="photobooth">
1111
<div class="controls">
1212
<button onClick="takePhoto()">Take Photo</button>
13-
<!-- <div class="rgb">
13+
<div class="rgb">
1414
<label for="rmin">Red Min:</label>
1515
<input type="range" min=0 max=255 name="rmin">
1616
<label for="rmax">Red Max:</label>
@@ -29,7 +29,7 @@
2929
<input type="range" min=0 max=255 name="bmin">
3030
<label for="bmax">Blue Max:</label>
3131
<input type="range" min=0 max=255 name="bmax">
32-
</div> -->
32+
</div>
3333
</div>
3434

3535
<canvas class="photo"></canvas>

0 commit comments

Comments
 (0)