Skip to content

The last half! #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions 14 - JavaScript References VS Copying/index-START.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@

<script>
// start with strings, numbers and booleans
// let age = 100;
// let age2 = age;
// console.log(age, age2);
// age = 200;
// console.log(age, age2);

// let name = 'Wes';
// let name2 = name;
// console.log(name, name2);
// name = 'wesley';
// console.log(name, name2);

// Let's say we have an array
const players = ['Wes', 'Sarah', 'Ryan', 'Poppy'];

// and we want to make a copy of it.
const team = players;

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

// however what happens when we update that array?

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

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

// one way

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

// or use the new ES6 Spread
const team4 = [...players];
team4[3] = 'heeee hawww';
console.log(team4);

const team5 = Array.from(players);

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

Expand All @@ -43,13 +64,35 @@
};

// and think we make a copy:
// const captain = person;
// captain.number = 99;

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

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

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

const wes = {
name: 'Wes',
age: 100,
social: {
twitter: '@wesbos',
facebook: 'wesbos.developer'
}
};

console.clear();
console.log(wes);

const dev = Object.assign({}, wes);

const dev2 = JSON.parse(JSON.stringify(wes));


</script>

</body>
Expand Down
46 changes: 44 additions & 2 deletions 15 - LocalStorage/index-START.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,53 @@ <h2>LOCAL TAPAS</h2>
<script>
const addItems = document.querySelector('.add-items');
const itemsList = document.querySelector('.plates');
const items = [];
const items = JSON.parse(localStorage.getItem('items')) || [];

function addItem(e) {

e.preventDefault()

const text = (this.querySelector('[name=item]')).value;
const item = {
text,
done: false
};

items.push(item);
populateList(items, itemsList);
localStorage.setItem('items', JSON.stringify(items));
this.reset();
}

function populateList(plates = [], platesList) {
platesList.innerHTML = plates.map((plate, i) => {
return `
<li>
<input type="checkbox" data-index=${i} id="item${i}" ${plate.done ? 'checked' : ''} />
<label for="item${i}">${plate.text}</label>
</li>
`;
}).join('');
}

function toggleDone(e) {
if (!e.target.matches('input')) return; // skip this unless it's an input
const el = e.target;
const index = el.dataset.index;
items[index].done = !items[index].done;
localStorage.setItem('items', JSON.stringify(items));
populateList(items, itemsList);
}

addItems.addEventListener('submit', addItem);

itemsList.addEventListener('click', toggleDone);

populateList(items, itemsList);


</script>


</body>
</html>

29 changes: 29 additions & 0 deletions 16 - Mouse Move Shadow/index-START.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,35 @@ <h1 contenteditable>🔥WOAH!</h1>
</style>

<script>

const hero = document.querySelector('.hero');
const text = hero.querySelector('h1');
const walk = 900;

function shadow(e) {
const { offsetWidth: width, offsetHeight: height } = hero;
let {offsetX: x, offsetY: y } = e;

if (this !== e.target) {
x = x + e.target.offsetLeft;
y = y + e.target.offsetTop;
}

const xWalk = Math.round((x / width * walk) - (walk / 2));
const yWalk = Math.round((y / height * walk) - (walk / 2));

text.style.textShadow = `
${xWalk}px ${yWalk}px 0 rgba(255,0,255,0.7),
${xWalk * -1}px ${yWalk}px 0 rgba(0,255,255,0.7),
${yWalk}px ${xWalk * -1}px 0 rgba(0,255,0,0.7),
${yWalk * -1}px ${xWalk}px 0 rgba(0,0,255,0.7)
`;

}

hero.addEventListener('mousemove', shadow);


</script>
</body>
</html>
14 changes: 12 additions & 2 deletions 17 - Sort Without Articles/index-START.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
padding: 0;
box-shadow: 0 0 0 20px rgba(0, 0, 0, 0.05);
}

#bands li {
border-bottom: 1px solid #efefef;
padding: 20px;
}

#bands li:last-child {
border-bottom: 0;
}
Expand All @@ -48,6 +48,16 @@
<script>
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'];

function strip(bandName) {
return bandName.replace(/^(a |the |an )/i, '').trim();
}

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

document.querySelector('#bands').innerHTML =
sortedBands
.map(band => `<li>${band}</li>`)
.join('');

</script>

Expand Down
20 changes: 20 additions & 0 deletions 18 - Adding Up Times with Reduce/index-START.html
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,26 @@
</li>
</ul>
<script>

const timeNodes = Array.from(document.querySelectorAll('[data-time]'));

const seconds = timeNodes
.map(node => node.dataset.time)
.map(timeCode => {
const [mins, secs] = timeCode.split(':').map(parseFloat);
return (mins * 60) + secs;
})
.reduce((total, vidSeconds) => total + vidSeconds);

let secondsLeft = seconds;
const hours = Math.floor(secondsLeft / 3600);
secondsLeft = secondsLeft % 3600;

const mins = Math.floor(secondsLeft / 60);
secondsLeft = secondsLeft % 60;

console.log(hours, mins, secondsLeft);

</script>
</body>
</html>
4 changes: 2 additions & 2 deletions 19 - Webcam Fun/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<div class="photobooth">
<div class="controls">
<button onClick="takePhoto()">Take Photo</button>
<!-- <div class="rgb">
<div class="rgb">
<label for="rmin">Red Min:</label>
<input type="range" min=0 max=255 name="rmin">
<label for="rmax">Red Max:</label>
Expand All @@ -29,7 +29,7 @@
<input type="range" min=0 max=255 name="bmin">
<label for="bmax">Blue Max:</label>
<input type="range" min=0 max=255 name="bmax">
</div> -->
</div>
</div>

<canvas class="photo"></canvas>
Expand Down
Loading