Skip to content

Commit b375850

Browse files
committed
finished with wesbos#1, wesbos#6, wesbos#15
1 parent 508e476 commit b375850

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

01 - JavaScript Drum Kit/index-START.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,24 @@
5858
<audio data-key="76" src="sounds/tink.wav"></audio>
5959

6060
<script>
61+
function removeTransition(e) {
62+
if (e.propertyName !== 'transform') return;
63+
e.target.classList.remove('playing');
64+
}
6165

66+
function playSound(e) {
67+
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
68+
const key = document.querySelector(`div[data-key="${e.keyCode}"]`);
69+
if (!audio) return;
70+
71+
key.classList.add('playing');
72+
audio.currentTime = 0;
73+
audio.play();
74+
}
75+
76+
const keys = Array.from(document.querySelectorAll('.key'));
77+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
78+
window.addEventListener('keydown', playSound);
6279
</script>
6380

6481

06 - Type Ahead/index-START.html

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,49 @@
1515
</ul>
1616
</form>
1717
<script>
18-
const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
18+
const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
19+
20+
const cities = [];
21+
fetch(endpoint)
22+
.then(blob => blob.json())
23+
.then(data => cities.push(...data))
24+
25+
function findMatches(wordToMatch, cities){
26+
return cities.filter(place => {
27+
//here we need to figure out if the city or state matchings with what was searched
28+
const regex = new RegExp(wordToMatch, 'gi')
29+
return place.city.match(regex) || place.state.match(regex)
30+
})
31+
}
32+
33+
function numberWithCommas(x) {
34+
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
35+
}
36+
37+
function displayMatches(){
38+
const matchArray = findMatches(this.value, cities)
39+
const html = matchArray.map(place => {
40+
const regex = new RegExp(this.value, 'gi')
41+
const cityName = place.city.replace(regex, `<span class="hl">${this.value}</span>`)
42+
const stateName = place.state.replace(regex, `<span class="hl">${this.value}</span>`)
43+
return `
44+
<li>
45+
<span class="name">${cityName}, ${stateName}</span>
46+
<span class="population">${numberWithCommas(place.population)}</span>
47+
</li>
48+
`
49+
}).join('')
50+
suggestions.innerHTML = html
51+
}
52+
53+
const searchInput = document.querySelector('.search')
54+
const suggestions = document.querySelector('.suggestions')
55+
56+
searchInput.addEventListener('change', displayMatches)
57+
searchInput.addEventListener('keyup', displayMatches)
1958

2059
</script>
2160
</body>
2261
</html>
62+
63+
<!-- //fetch returns a promise -->

15 - LocalStorage/index-START.html

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

3373
</script>
3474

0 commit comments

Comments
 (0)