This content originally appeared on DEV Community and was authored by VirtualSobriety
Hey all and welcome back to another day of Wes Bos's JavaScript30! Alright...it's been over 2 weeks since my last post and that’s pretty sad. That being said, I did put in my notice at my current job and they have been running me into the ground so I haven't had as much time to work on my coding recently...but now that I am officially part time you can bet your asses that these posts are going to start coming more regularly and possibly more diverse as I will have time to work on other projects apart from just this one. However, you didn't come here for updates on my life you are here to see more about this course! So let's begin!
Array Cardio day 2 was NOTHING compared to day one. I won't lie. I was absolutely dreading going into this challenge based on how the first one went. The first course had me googling constantly and jumping through hoops. It truly felt like a workout, whereas this was more of a stretch or maybe a yoga session. The first part of Array Cardio day 2 involved using Array.prototype.some()
and Array.protoype.every()
. We were given an object within an array that held a list of people and the years they were born. With this information we were asked to figure out if at least one person was over 19 and then if every person was over 19.
const people = [
{ name: 'Wes', year: 1988 },
{ name: 'Kait', year: 1986 },
{ name: 'Irv', year: 1970 },
{ name: 'Lux', year: 2015 }
];
// Some and Every Checks
// Array.prototype.some() // is at least one person 19 or older?
const isAdult = people.some(person => ((new Date())
.getFullYear()) - person.year >= 19 )
console.log({isAdult})
// Array.prototype.every() // is everyone 19 or older?
const areAdult = people.every(person => {
const currentYear = (new Date()).getFullYear();
return currentYear - person.year >= 19
}
)
console.log(areAdult)
This part of the challenge made me feel pretty damn good about myself, I won't lie about that. After a quick google search I had my answer on how to use Array.prototype.some()
and that also directly applied to .every()
. I absolutely blew that out of the water and felt like I would be able to finish this challenge in record time. It turns out I was both right and wrong...
The second and, somehow, the final part of this challenge involved Array.prototype.find()
and Array.prototype.findIndex()
. We were given another object within an array but this time it had a list of comments for some kind of restaurant reviews that all had their own id numbers to help differentiate them. Just like before doing a quick google search showed me how to use .find()
and how to use it well, but it appears it wasn't exactly what Wes was looking for. I only used .find()
while using console.log
when referencing a function I made that called the id based on the number given. I guess I could argue that it was similar to what he did...kind of...you know, considering we both came up with the same result. But I don't know, I think I like my code better than his in this case.
const comments = [
{ text: 'Love this!', id: 523423 },
{ text: 'Super good', id: 823423 },
{ text: 'You are the best', id: 2039842 },
{ text: 'Ramen is my fav food ever', id: 123523 },
{ text: 'Nice Nice Nice!', id: 542328 }
];
// Find is like filter, but instead returns just the one you are looking for
// find the comment with the ID of 823423
function hasId(idNumber) {
return idNumber.id === 823423
}
console.log (comments.find(hasId))
// Array.prototype.findIndex()
// Find the comment with this ID
// delete the comment with the ID of 823423
function thisId(idNumber) {
return idNumber.id === 823423
}
console.log (comments.findIndex(thisId))
const index = comments.findIndex(comment => comment.id === 823423)
const newComments = [
...comments.slice(0, index),
...comments.slice(index + 1)
]
console.table(newComments)
It also turns out that I was unclear about what he wanted us to do for the final part of the challenge. While having us use findIndex()
he also wanted us to delete the comment with the same id that we just found before. I just went through VScode and deleted the comment manually. To be fair, I figured he just wanted to show us what it would return if the comment was no longer there. It turns out he wanted us to use either .splice
or .slice
to delete the comment via new code (which makes more sense for this exercise) and then have us access the new array without that specific comment in it. So...yeah...I didn't get to that point because I just deleted it manually. But I did go back and coded along with him to see how that would be possible with new lines of code.
So there it is. Array Cardio Day 2. I'm relieved to know that there isn't a Day 3 of this. That isn't me saying that I couldn't use more practice with arrays and how you can interact with them. It's just as far as this course has gone these were probably the least fun so far. Well be on the lookout for the next lesson I will be covering, hopefully it will be out either this week or early next week since I will have more time! Regardless I hope you're ready for JavaScript30: Fun With HTML5 Canvas!
This content originally appeared on DEV Community and was authored by VirtualSobriety
VirtualSobriety | Sciencx (2024-07-10T19:01:19+00:00) JavaScript 30 – 7 Array Cardio Day 2. Retrieved from https://www.scien.cx/2024/07/10/javascript-30-7-array-cardio-day-2/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.