Skip to content

Commit e0fe341

Browse files
committed
Updates
1 parent 7068292 commit e0fe341

8 files changed

Lines changed: 242 additions & 6 deletions

File tree

01 - JavaScript Drum Kit/index-FINISHED.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,28 @@
6060
<script>
6161
function removeTransition(e) {
6262
if (e.propertyName !== 'transform') return;
63+
6364
e.target.classList.remove('playing');
6465
}
6566

6667
function playSound(e) {
6768
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
6869
const key = document.querySelector(`div[data-key="${e.keyCode}"]`);
70+
6971
if (!audio) return;
7072

7173
key.classList.add('playing');
74+
7275
audio.currentTime = 0;
7376
audio.play();
7477
}
7578

7679
const keys = Array.from(document.querySelectorAll('.key'));
7780
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
78-
window.addEventListener('keydown', playSound);
79-
</script>
8081

82+
document.addEventListener('keydown', playSound);
83+
84+
</script>
8185

8286
</body>
8387
</html>

02 - JS and CSS Clock/index-START.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,37 @@
6262
background: black;
6363
position: absolute;
6464
top: 50%;
65+
transform-origin: 100%;
66+
transform: rotate(90deg);
67+
transition: all 0.05s;
68+
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
6569
}
6670

6771
</style>
6872

6973
<script>
74+
const secondHand = document.querySelector('.second-hand');
75+
const minsHand = document.querySelector('.min-hand');
76+
const hourHand = document.querySelector('.hour-hand');
7077

78+
function setDate() {
79+
const now = new Date();
7180

81+
const seconds = now.getSeconds();
82+
const secondsDegrees = (seconds / 60) * 360 + 90;
83+
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
84+
85+
const mins = now.getMinutes();
86+
const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90;
87+
minsHand.style.transform = `rotate(${minsDegrees}deg)`;
88+
89+
const hour = now.getHours();
90+
const hourDegrees = ((hour / 12) * 360) + ((mins/60)*30) + 90;
91+
hourHand.style.transform = `rotate(${hourDegrees}deg)`;
92+
}
93+
94+
setInterval(setDate, 1000);
95+
setDate();
7296
</script>
7397
</body>
7498
</html>

04 - Array Cardio Day 1/index-START.html

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,70 @@
3131

3232
// Array.prototype.filter()
3333
// 1. Filter the list of inventors for those who were born in the 1500's
34+
const fifteen = inventors.filter(inventor => (inventor.year >= 1500 && inventor.year < 1600));
35+
console.table(fifteen);
3436

3537
// Array.prototype.map()
3638
// 2. Give us an array of the inventors first and last names
39+
const names = inventors.map(inventor => {
40+
return `${inventor.first} ${inventor.last}`;
41+
});
42+
console.table(names);
3743

3844
// Array.prototype.sort()
3945
// 3. Sort the inventors by birthdate, oldest to youngest
46+
const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1);
47+
console.table(ordered);
4048

4149
// Array.prototype.reduce()
4250
// 4. How many years did all the inventors live all together?
51+
const totalYears = inventors.reduce((total, inventor) => {
52+
return total + (inventor.passed - inventor.year);
53+
}, 0);
54+
console.log(totalYears);
4355

4456
// 5. Sort the inventors by years lived
57+
const oldest = inventors.sort(function(a, b) {
58+
const aAge = a.passed - a.year;
59+
const bAge = b.passed - b.year;
60+
61+
return aAge > bAge ? -1 : 1;
62+
});
63+
console.table(oldest);
4564

4665
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
4766
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
4867

68+
// const category = document.querySelector('.mw-category');
69+
// const links = Array.from(category.querySelectorAll('a'));
70+
// const de = links
71+
// .map(link => link.textContent)
72+
// .filter(streetName => streetName.includes('de'));
73+
4974

5075
// 7. sort Exercise
51-
// Sort the people alphabetically by last name
76+
// Sort the people alphabetically by first name
77+
const sortedPeople = people.sort(function(a, b){
78+
const [aLast, aFirst] = a.split(', ');
79+
const [bLast, bFirst] = b.split(', ');
80+
81+
return aFirst > bFirst ? 1 : -1;
82+
});
83+
console.table(sortedPeople);
5284

5385
// 8. Reduce Exercise
5486
// Sum up the instances of each of these
5587
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
5688

89+
const transportation = data.reduce(function(hash, item) {
90+
if (!hash[item]) {
91+
hash[item] = 0;
92+
}
93+
hash[item]++;
94+
95+
return hash;
96+
}, {});
97+
console.table(transportation);
5798
</script>
5899
</body>
59100
</html>

06 - Type Ahead/index-START.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,43 @@
1717
<script>
1818
const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
1919

20+
const cities = [];
21+
fetch(endpoint).then(response => response.json()).then(json => console.log(cities.push(...json)))
22+
23+
function findMatches(wordToMatch, cities) {
24+
return cities.filter(place => {
25+
const regex = new RegExp(wordToMatch, 'gi');
26+
return place.city.match(regex) || place.state.match(regex);
27+
});
28+
}
29+
30+
function numberWithCommas(x) {
31+
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
32+
}
33+
34+
function displayMatches() {
35+
const matchArray = findMatches(this.value, cities);
36+
const html = matchArray.map(place => {
37+
const regex = new RegExp(this.value, 'gi');
38+
const cityName = place.city.replace(regex, `<span class="hl">${this.value}</span>`)
39+
const stateName = place.state.replace(regex, `<span class="hl">${this.value}</span>`)
40+
41+
return `
42+
<li>
43+
<span class="name">${cityName}, ${stateName}</span>
44+
<span class="population">${numberWithCommas(place.population)}</span>
45+
</li>
46+
`;
47+
}).join('');
48+
49+
suggestions.innerHTML = html;
50+
}
51+
52+
const searchInput = document.querySelector('.search');
53+
const suggestions = document.querySelector('.suggestions');
54+
55+
searchInput.addEventListener('change', displayMatches);
56+
searchInput.addEventListener('keyup', displayMatches);
2057
</script>
2158
</body>
2259
</html>

07 - Array Cardio Day 2/index-START.html

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,35 @@
2525
];
2626

2727
// Some and Every Checks
28-
// Array.prototype.some() // is at least one person 19 or older?
28+
// Array.prototype.some() // is at least one person 19 or older? Ruby: any
29+
const anyAudults = people.some(person => ((new Date()).getFullYear() - person.year) > 19);
30+
console.log({anyAudults});
2931
// Array.prototype.every() // is everyone 19 or older?
32+
const allAudult = people.every(person => ((new Date()).getFullYear() - person.year) > 19);
33+
console.log({allAudult});
3034

3135
// Array.prototype.find()
3236
// Find is like filter, but instead returns just the one you are looking for
3337
// find the comment with the ID of 823423
38+
// like: Ruby array #find
39+
const comment = comments.find(comment => comment.id === 823423);
40+
console.log(comment);
3441

3542
// Array.prototype.findIndex()
3643
// Find the comment with this ID
3744
// delete the comment with the ID of 823423
45+
// like: Ruby array #find_index
46+
const index = comments.findIndex(comment => comment.id === 823423);
47+
console.log({index});
48+
49+
// const deleted_comment = comments.splice(index, 1) // in place
50+
// console.log(deleted_comment);
51+
52+
const newComments = [
53+
...comments.slice(0, index),
54+
...comments.slice(index + 1)
55+
]
56+
console.table(newComments); // This wont change the comments
3857

3958
</script>
4059
</body>

08 - Fun with HTML5 Canvas/app.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const canvas = document.querySelector("#draw");
2+
const ctx = canvas.getContext("2d");
3+
4+
canvas.width = window.innerWidth;
5+
canvas.height = window.innerHeight;
6+
7+
ctx.strokeStyle = "#BADA55";
8+
ctx.lineJoin = "round";
9+
ctx.lineCap = "round";
10+
ctx.lineWidth = 10;
11+
// ctx.globalCompositeOperation = "multiply";
12+
13+
let isDrawing = false;
14+
let lastX = 0;
15+
let lastY = 0;
16+
let hue = 0;
17+
18+
function draw(e) {
19+
if (!isDrawing) return; // when not moused down.
20+
console.log(e);
21+
22+
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
23+
ctx.beginPath();
24+
// start from
25+
ctx.moveTo(lastX, lastY);
26+
// go to
27+
ctx.lineTo(e.offsetX, e.offsetY);
28+
ctx.stroke();
29+
30+
[lastX, lastY] = [e.offsetX, e.offsetY];
31+
32+
hue++;
33+
if (hue >= 360) {
34+
hue = 0;
35+
}
36+
}
37+
canvas.addEventListener("mousedown", e => {
38+
isDrawing = true;
39+
[lastX, lastY] = [e.offsetX, e.offsetY];
40+
});
41+
42+
canvas.addEventListener("mousemove", draw);
43+
44+
canvas.addEventListener("mouseup", () => (isDrawing = false));
45+
canvas.addEventListener("mouseout", () => (isDrawing = false));

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
</head>
77
<body>
88
<canvas id="draw" width="800" height="800"></canvas>
9-
<script>
10-
</script>
9+
<script src="./app.js"></script>
1110

1211
<style>
1312
html, body {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
let countdown;
2+
const timerDisplay = document.querySelector(".display__time-left");
3+
const endTime = document.querySelector(".display__end-time");
4+
const buttons = document.querySelectorAll("[data-time]");
5+
6+
function timer(seconds) {
7+
clearInterval(countdown);
8+
9+
const now = Date.now();
10+
const then = now + seconds * 1000; // ms
11+
12+
displayTimeLeft(seconds);
13+
displayEndTime(then);
14+
15+
countdown = setInterval(() => {
16+
const secondsLeft = Math.round((then - Date.now()) / 1000);
17+
18+
if (secondsLeft < 0) {
19+
clearInterval(countdown);
20+
return;
21+
}
22+
displayTimeLeft(secondsLeft);
23+
}, 1000);
24+
}
25+
// setInterval() does not run sometimes
26+
// On iOS when you are scrolling, it will pause all of your intervals
27+
28+
function displayTimeLeft(seconds) {
29+
const minutes = Math.floor(seconds / 60);
30+
const remainderSeconds = seconds % 60;
31+
32+
const display = `${minutes}:${
33+
remainderSeconds < 10 ? "0" : ""
34+
}${remainderSeconds}`;
35+
36+
timerDisplay.textContent = display;
37+
38+
document.title = display;
39+
40+
console.log({ minutes, remainderSeconds });
41+
}
42+
43+
function displayEndTime(timestamp) {
44+
const end = new Date(timestamp);
45+
const hour = end.getHours();
46+
const minutes = end.getMinutes();
47+
48+
endTime.textContent = `Be Back At: ${hour}:${
49+
minutes < 10 ? "0" : ""
50+
}${minutes}`;
51+
}
52+
53+
function startTimer(event) {
54+
timer(event.target.dataset.time);
55+
}
56+
57+
buttons.forEach(button => button.addEventListener("click", startTimer));
58+
59+
document.customForm.addEventListener("submit", function(e) {
60+
e.preventDefault();
61+
62+
const minutes = document.customForm.minutes.value;
63+
64+
timer(minutes * 60);
65+
66+
e.target.reset();
67+
});

0 commit comments

Comments
 (0)