Skip to content

Commit ba1e825

Browse files
committed
Finished JS30 exercise wesbos#11 - wesbos#18.
1 parent 0ff5e27 commit ba1e825

File tree

11 files changed

+433
-52
lines changed

11 files changed

+433
-52
lines changed

11 - Custom Video Player/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</head>
88
<body>
99

10-
<div class="player">
10+
<div class="player">
1111
<video class="player__video viewer" src="https://player.vimeo.com/external/194837908.sd.mp4?s=c350076905b78c67f74d7ee39fdb4fef01d12420&profile_id=164"></video>
1212

1313
<div class="player__controls">
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Get elements */
2+
const player = document.querySelector(".player");
3+
const video = player.querySelector(".viewer");
4+
const progress = player.querySelector(".progress");
5+
const progressBar = player.querySelector(".progress__filled");
6+
const toggle = player.querySelector(".toggle");
7+
const skipBtns = player.querySelectorAll("[data-skip]");
8+
const sliders = player.querySelectorAll(".player__slider");
9+
10+
/* Functions and logic */
11+
function togglePlay(e) {
12+
if (video.paused) {
13+
video.play();
14+
} else {
15+
video.pause();
16+
}
17+
}
18+
19+
function updateBtn() {
20+
toggle.textContent = this.paused ? "▶" : "❚❚";
21+
}
22+
23+
function skip() {
24+
const skipValue = this.dataset.skip;
25+
video.currentTime += parseFloat(skipValue);
26+
}
27+
28+
function handleRangeUpdate() {
29+
video[this.name] = this.value;
30+
}
31+
32+
function scrub(e) {
33+
const scrubTime = (e.offsetX / progress.offsetWidth) * video.duration;
34+
video.currentTime = scrubTime;
35+
}
36+
37+
function handleProgressBar() {
38+
const percent = (video.currentTime / video.duration) * 100;
39+
progressBar.style.flexBasis = `${percent}%`;
40+
}
41+
42+
/* Event listeners */
43+
video.addEventListener('click', togglePlay);
44+
video.addEventListener('play', updateBtn);
45+
video.addEventListener('pause', updateBtn);
46+
video.addEventListener('timeupdate', handleProgressBar);
47+
48+
progress.addEventListener('click', scrub);
49+
50+
toggle.addEventListener('click', togglePlay);
51+
52+
skipBtns.forEach(btn => btn.addEventListener('click', skip));
53+
54+
sliders.forEach(slider => slider.addEventListener('change', handleRangeUpdate));

11 - Custom Video Player/style.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ body {
9999

100100
.progress__filled {
101101
width:50%;
102-
background:#ffc600;
102+
background:#ad0a04;
103103
flex:0;
104-
flex-basis:50%;
104+
flex-basis:0%;
105105
}
106106

107107
/* unholy css to style input type="range" */
@@ -148,7 +148,7 @@ input[type=range]::-moz-range-track {
148148
}
149149
input[type=range]::-moz-range-thumb {
150150
box-shadow: 0 0 0 rgba(0, 0, 0, 0), 0 0 0 rgba(13, 13, 13, 0);
151-
height: 15px;
151+
height: 5px;
152152
width: 15px;
153153
border-radius: 50px;
154154
background: #ffc600;

12 - Key Sequence Detection/index-START.html

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

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

Lines changed: 150 additions & 33 deletions
Large diffs are not rendered by default.

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
// and we want to make a copy of it.
1616

1717
// You might think we can just do something like this:
18+
//const team = players;
1819

1920
// however what happens when we update that array?
21+
//team[2] = 'Jon';
2022

2123
// now here is the problem!
2224

@@ -27,10 +29,15 @@
2729
// So, how do we fix this? We take a copy instead!
2830

2931
// one way
32+
team2 = players.slice();
3033

3134
// or create a new array and concat the old one in
35+
team3 = [].concat(players);
3236

3337
// or use the new ES6 Spread
38+
team4 = [...players];
39+
40+
team5 = Array.from(players);
3441

3542
// now when we update it, the original one isn't changed
3643

@@ -43,13 +50,35 @@
4350
};
4451

4552
// and think we make a copy:
53+
const man1 = person;
54+
man1.age = 20;
55+
console.log(person, man1);
4656

4757
// how do we take a copy instead?
58+
const man2 = Object.assign({}, person, {age : 45});
59+
console.log(person, man2);
4860

4961
// We will hopefully soon see the object ...spread
5062

5163
// 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.
5264

65+
// Poor man's cloneDeep
66+
const guy = {
67+
name : 'He Man',
68+
details : {
69+
number : [
70+
9878382782,
71+
8391738172
72+
],
73+
email: "he@man.com",
74+
facebook: "he.man"
75+
}
76+
}
77+
78+
const newGuy = JSON.parse(JSON.stringify(guy));
79+
newGuy.details.email = "new@guy.com";
80+
console.log(guy, newGuy);
81+
5382
</script>
5483

5584
</body>

15 - LocalStorage/index-START.html

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,49 @@ <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")) || [];
3232

33+
addItems.addEventListener('submit', addItemToList);
34+
itemsList.addEventListener('click', checkboxEvent);
35+
36+
function addItemToList(e) {
37+
e.preventDefault();
38+
name = (this.querySelector('[name=item]')).value;
39+
item = {
40+
name,
41+
done: false
42+
};
43+
44+
items.push(item);
45+
save();
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" id="item${i}" data-index=${i} ${plate.done ? 'checked' : ''}/>
54+
<label for="item${i}">${plate.name}</label>
55+
</li>
56+
`;
57+
}).join('');
58+
}
59+
60+
function save() {
61+
localStorage.setItem("items", JSON.stringify(items));
62+
populateList(items, itemsList);
63+
}
64+
65+
function checkboxEvent(e) {
66+
if (!e.target.matches('input')) return;
67+
const el = e.target;
68+
const index = el.dataset.index;
69+
items[index].done = !items[index].done;
70+
save();
71+
}
72+
73+
populateList(items, itemsList);
3374
</script>
3475

3576

16 - Mouse Move Shadow/index-start.html

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

3737
<script>
38+
const hero = document.querySelector('.hero');
39+
const h1 = hero.querySelector('h1');
40+
const maxOffset = 120;
41+
const shadowColor = 'rgba(150, 50, 120, 0.4)';
42+
h1.style.textShadow = `0px 0px 0px ${shadowColor}`;
43+
44+
hero.addEventListener('mousemove', moveShadow);
45+
46+
function moveShadow(e) {
47+
const {offsetWidth : width, offsetHeight : height} = hero;
48+
let {offsetX : x, offsetY : y} = e;
49+
50+
if (this !== e.target) {
51+
x = x + e.target.offsetLeft;
52+
y = y + e.target.offsetTop;
53+
}
54+
55+
const xWalk = Math.round((x/width * maxOffset) - (maxOffset/2));
56+
const yWalk = Math.round((y/height * maxOffset) - (maxOffset/2));
57+
const blur = (Math.abs(xWalk) + Math.abs(yWalk))/(maxOffset) * 40;
58+
console.log(xWalk, yWalk);
59+
60+
h1.style.textShadow = `${-xWalk}px ${-yWalk}px ${blur}px ${shadowColor}`;
61+
}
62+
3863
</script>
3964
</body>
4065
</html>

17 - Sort Without Articles/index-START.html

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>Sort Without Articles</title>
67
</head>
8+
79
<body>
810

911
<style>
@@ -26,10 +28,12 @@
2628
padding: 0;
2729
box-shadow: 0 0 0 20px rgba(0, 0, 0, 0.05);
2830
}
31+
2932
#bands li {
3033
border-bottom: 1px solid #efefef;
3134
padding: 20px;
3235
}
36+
3337
#bands li:last-child {
3438
border-bottom: 0;
3539
}
@@ -38,16 +42,28 @@
3842
color: #ffc600;
3943
text-decoration: none;
4044
}
41-
4245
</style>
4346

4447
<ul id="bands"></ul>
4548

46-
<script>
47-
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'];
49+
<script>
50+
const bands = ['The Plot in You', 'The Devil Wears Prada', 'Pierce the Veil', 'Norma Jean', 'The Bled',
51+
'Say Anything', 'The Midway State', 'We Came as Romans', 'Counterparts', 'Oh, Sleeper', 'A Skylit Drive',
52+
'Anywhere But Here', 'An Old Dog'
53+
];
54+
55+
function strip(bandName) {
56+
return bandName.replace(/^(a|the|an)\s/i, '');;
57+
}
4858

59+
const sortedBands = bands.sort((a, b) => strip(a) > strip(b) ? 1 : -1);
4960

50-
</script>
61+
document.querySelector('#bands').innerHTML =
62+
sortedBands
63+
.map(band => `<li>${band}</li>`)
64+
.join('');
65+
</script>
5166

5267
</body>
53-
</html>
68+
69+
</html>

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>Videos</title>
67
</head>
8+
79
<body>
810
<ul class="videos">
911
<li data-time="5:43">
@@ -177,11 +179,28 @@
177179
<li data-time="1:56">
178180
Video 57
179181
</li>
180-
<li data-time="4:04">
182+
<li data-time="6:06">
181183
Video 58
182184
</li>
183185
</ul>
184-
<script>
185-
</script>
186+
<script>
187+
listItems = document.querySelectorAll('li');
188+
listArray = [...listItems];
189+
const totalTime = listArray.reduce((total, item) => {
190+
const [minute, seconds] = item.dataset.time.split(':').map(parseFloat);
191+
return total += minute * 60 + seconds;
192+
}, 0);
193+
194+
const totalSecs = totalTime % 60;
195+
let totalMins = Math.floor(totalTime / 60);
196+
let totalHrs = 0;
197+
if (totalMins >= 60) {
198+
totalHrs = Math.floor(totalMins / 60);
199+
totalMins = Math.floor(totalMins % 60);
200+
}
201+
202+
console.log(`${totalHrs}:${totalMins}:${totalSecs}`);
203+
</script>
186204
</body>
187-
</html>
205+
206+
</html>

0 commit comments

Comments
 (0)