Today we sweat! In order to do the rest of the JavaScript30 exercises, we need to get really good at working with Arrays in JavaScript. Grab all the exercises and code samples at JavaScript30.com
I have been coming back to this video whenever I get kinda stuck and especially this comment, every time I come back, I refresh my knowledge and it gets a little bit more cemented into my brain. Thanks!
10:08 I've just the started files and the exact same code, but instead of 523, I get 861. It took me too long to realize that I have more people in my inventors. I'm going to post my comment in case someone else also has more inventors on their list and wonders why their answer is different too.
Here from the Odin Project. If you're having trouble understanding these, look at the documentation behind each method and really understand what its doing since he doesn't explain it in this video, he just explains what it does and how to use it. I spent 3 hours looking at the documentation for each one to truly understand the behind-the-scenes.
I might be late to the party but I found your videos via the Odin Project. I really like that this gives me a few things to practice as I try and learn how to make JavaScript do what I want it to do.
i share same feelings. i started odin project after wasting 4 years of not doing my best in my comp sci degree but here i am giving my all after i graduated. lets do this sir!!!!!!! also good luck to everyone who came this far because most just quit after a few lessons. its a marathon and not a sprint. we can do it. lessss goooo!!!!!!!!!!!!!!!!!!
I am in the calculator almost, everything so far was hard, and now watching all these array methods, my brain cannot memorize everything, and how i will be able to think what i need for each action! Its literally impossible for someone coming with zero background in computer science and maths.. Any tips ?
@@youtubejproasters1224 experiment, if you have an idea. Go for it and explore it. I was on the calculator as well. I asked lots of questions and experimented with my thought process and pseudocode. Currently on the Todo List Project, almost done with it. You got this
@@jimmyjimenez8599 I'm here from Odin Project! Hope you guys kept going. Every time it sends us to a video I notice fewer of us in the comments. I'm going to stick with it :)
This guy is brilliant, he is very talented but still feels how tough it is for beginners and showcases his coding for all levels. Doesn't give it the big I AM like a lot of programmers do but is humble and helpful to all. Bravo.
I'm barely keeping up with what's happening in this video but I'm very impressed by it all! I hope I can learn and master all these skills with time and practice!
instead of returning 1 and -1 you can also do this using, // ordered list from oldest to youngest const ordered = inventors.sort((a, b) => a.year - b.year); console.table(ordered); // sort by years lived const oldest = inventors.sort( (a, b) => b.passed - b.year - (a.passed - a.year) ); console.table(oldest);
@@carolinaserrano8267 from the documentation: with values (a,b) => ... if a is less than b a negative value should be passed, positive if otherwise. What this tells the sort() function is whether or not it should swap these two elements, and it will iterate over everything with that function until nothing moves anymore, i.e. the array is sorted as per the function's specification. What's happening under the hood is "bubble sort", essentially just this process of sweeping over an array and swapping 2 values if they're out of order, and sweeping over it again and again until nothing gets moved
Loved the video.But loved even more what the people from odin project are commenting. They share what is being missed here and what to do about it with respect to the course.Thank you to all these peeps.Thats how a healthy community should be.
For sorting the people by last name, since they have the last name first, just using people.sort() like that seems to work. It is nice to learn about splitting them up first and separating the specific parts to sort them.
The method outlined would be required if we were being asked to sort by first name, I suppose. I am not familiar with the syntax used in the arrow function, either, where the variables `aLast` and `aFirst` are being declared. Looks like an array is being declared with two elements, but as a relative beginner, I wish this had been explained.
I found this specific one very confusing, to the point where i couldn't see how the people were sorted at the end even. Looked like the last names were still in a random order???
The reason this works is because a comma is U+002C, while the first unicode letter, A, is U+0041, which is numerically lower. This means that in the case of a shorter but otherwise identical name the decision isn't happening because of shorter length (which it should), but because the separator by chance is of a lower value than any letter a longer name could possibly contain, thus it gets sorted as if it were shorter. Of course, commas are common as separators, but one could absolutely use | or ¸ to do this and the whole thing would fall apart then
@@ZakvonMammel I'm MILES from where I was when this comment was made, actually! I started my first job as a developer last month. These array methods are no longer a foreign language and I've become very comfortable using them. Reduce can still be annoying for me because of the accumulator if I remember correctly but yeah - night and day difference a year makes, haha.
@@ZakvonMammel Ah God might be a little strong due to the way it kind of symbolizes some sort of idolization but Wes is DAMN good, that's for sure! He certainly seems as though he's put in the work and can write clean, efficient code!
like!! Im not lying nor exaggerating when I say that I indeed learned more of than what we are supposed to learn with this perfectly done video thanks to each of those single little extra steps you bother to quickly show to us. VERY important ones, regards!
filter -> takes each element in an array and it applies a conditional statement against it. If this conditional returns true, the element gets pushed to the output array. If the condition returns false, the element does not get pushed to the output array. map -> used for creating a new array from an existing one, applying a function to each one of the elements of the first array reduce -> deduct the array into a single solution, can return anything depends on the accumulator you have (either a number, an array or an object...) sort -> arrange or reverse the original array based on the condition These are all array method that would not change the original array, but return a new array or solution. --> console.table is really nice way to show the data in console
Thanks for the tutorial, i like when you use first function then when you make it arrow function, it makes it easy for beginners to follow and understand.
Just wanted to say these video's are great! I'm only a few months into learning anything programming and this one in particular has really helped me understand Array methods, Thank-you! :)
The Odin Project team, please keep going! If you are coding along please pay attention that your total years lived might not match - that is okay, unlike the video the correct answer is 861 for your case(because the number of inventors have gone up in the code.)
Hot tip for young players people.sort() works for 7. The default behaviour of sort() with no parameters is to sort by ASCII. Because the names Strings already start with the surname, literally all you need to do is use people.sort(); Also, for numerical sort, sort((a, b) => {return a - b}) works. Because if a is bigger, you return a negative number. If equal, you return 0. If smaller, you return a positive number. This is all in the docs.
Thanks for confirming my initial instinct. I was confused as to why the video went through all the trouble of splitting everything when all the names were already organized with the last name in the beginning.
For everyone saying he doesn't explain, yes he doesn't. Pause the video, go read about the method/function in the documentation, make stuff with what you have learned from it then start the video again, if you dont do this you wont learn. Also, shout-out to the Odin Project!
Another invaluable video. I'm really enjoying this 30 day JS course. With every video, i understand how things work in JS. Plus I learn much simpler and better ways to do things with JS. I cannot thank enough!😁
Thank you so much!!!!!!!!!!! I screen-shotted each of the exercises to copy and practise in my own time..... I have been a bit overwhelmed by es6 probably because my javascript isn't yet rock solid
7:39 The sort() by birthday part can be written without even using ternary operator like this: const ordered = inventors.sort( (a,b) => a.year - b.year )
Hands down one of the great teachers on JavaScript, now I understand why everybody I asked about who's course or material should I go for and everyone said 'Wes Bos'. Feeling lucky I found you and your courses.
About the sort function, you could actually shorten it event more by returning a.year - b.year and what is more important the sort function mutates the original variable so you could just console.table the inventors instead of assigning it to the new variable. Thanks for the video. Keep up the good work.
@@Admlass While solving these, I assign the inventors object to a new const and then log the results of that const. Is that much different to using the spread syntax?
For #7, you can rewrite the code using the logical OR (||) operator to set a default value if the key doesn't exist in the object. If it doesn't exist, set its value to 0 then add one. Else, add one to whatever the value currently is. const transportation = data.reduce((obj, item) => { obj[item] = (obj[item] || 0 ) + 1; return obj; }, {}); console.log(transportation);
In the last task we don't need to create key with the value of 0 and then increase it to 1 at the second line. Just create it with 1 and put increment statement inside 'else' . Ternary is even simpler: !obj[ item ] ? obj[ item ] = 1 : obj[ item ]++
The problem I have with these is that I always find the other way to solve the same problems without those methods so it's hard for me to remember all those methods when I'm working on my personal projects.
At 22:30. obj[item] = (obj[item]||0) + 1 would do the trick Also the sort function needs not to return 1 or -1, but merely a positive or negative value so ( a , b ) =>. a.year - b.year. would be ok
"sort inventors by birthdate, oldest to youngest" - keep in mind this means the lowest number (year) first, so ascending order. also if the sort returns less than 0, sort leaves a, b unchanged, but if it returns greater than 0, it sorts b before a. so if number 1 is greater than number 2, it sorts number 2 before number 1, otherwise if number 1 is less than number 2, it leaves them be. 4 - 2 = 2; => sort 2 before 4. 2 - 4 = -2 => leave 2 before 4.
@momoxiaoshizi another amazing playlist on functional programming in JS is: ru-vid.com/group/PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84 MPJ covers these methods and explains how they work so well! ... if you're interested.
For the 7th (sorting ppl by surnames) you don't need to split the data. You can simply sort it with a > b, since JavaScript would compare it char by char, according to the unicode table. Additional note: at first I was unsure how this would handle "," and " ", but since they have lower unicodes, it actually works well. I do think Wes's solution is cleaner/looks nicer, though.
tried the last sum up by myself first creating 5 different vars for each type of vehicle with a switch-case and then saw the obj implementation that you did.... atleast I'm learning
10:00 total is not undefined. If no accumulator is provided, it uses the first element in the array as default. The first element is an object and we are adding a number to that object. js converts that object to a string, and that's where [object Object] came from.
The video is 4 years old so I'm sure the methods have been updated since then, but it looks like the solution for 7: "Sort the people alphabetically by last name" would only need to be that complicated if the people array was not already structured as last name, first name, or if you needed to alphabetize by first name. Otherwise "people.sort()" will give you the same result without all the fuss.
Yea I don't understand why he went through all that, just made a one-liner lol. I don't see why it would be different years later either. const alpha = people.sort( (a, b) => a > b ? 1 : -1 )
I don't think assuming the array you are working with is always going to be structured in the same way would be good practice, you'd want to be more specific about what you were sorting by - it would also make it more clear to others reading the code what the intent of the code was originally (which may be years later).
Alot of times I found the code written before it was made more concise far more readable. Maybe thats because Ive done some Java programming before so im used to things being verbose, but for me being concise in the JS way of things comes at the cost of readability which would cost me time down the road when debugging code.
At 9:58 you said: "the first time around we don't have a total and it's equal to undefined". In fact, on the first call, the accumulator (total, in our case) value is equal to the first item in the array (the first inventor object, in our case). This why we got that weird log at first, because the function tried to add an object and a number. Now, if the initial value - the value passed as the second argument to the reduce function after the callback function - is provided, the accumulator value will be equal to this value (the zero, in our case).
Nice exercises! I was trying my own solutions before following along with yours. The reduce exercise is ambiguous about what it is asking for, however. The file asks to "sum up the instances of each of these." It might be more clearly worded as "return an object containing a key for each kind of vehicle with a value corresponding to the number of occurrences"
For anyone wondering why the end result of the exercise 7 is the same as the original array, it's because there is a confusion in the variable names inside the comparator function: const alpha = people.sort((personA, personB) => { const [lastA, firstA] = personA.split(', '); const [lastB, firstB] = personB.split(', '); return lastA > lastB ? 1 : -1; // We are comparing the FIRSTNAMES. }) // lastA = person A FIRSTNAME ; firstA = person A LASTNAME
The output is not alphabetically sorted as the naming is mistaken. It should be like this const[aFirst, aLast] = lastOne.split(', '); const[bFirst, bLast] = lastOne.split(', '); now, It will work fine. Btw, thanks for such a comprehensive tutorial. You are simply best :-O
If at 10:00 you're confused, there's a simpler (although a bit less succinct) way of doing the same thing: const yearsLived = inventors.map(inventor =>{ return (inventor.passed -inventor.year); }) const allTogether = yearsLived.reduce((a, b) => a + b); console.log(allTogether) So at first you create an array of years lived of each one with map, then on that array you do the simplest possible a + b. This way you don't have to add a 'starting'/default total value of 0.
I see some people say they couldn't do the Wikipedia one. It worked with this: let category = document.querySelector(".mw-category"); let links = Array.from(category.querySelectorAll("a")); let de = links .map((link) => link.innerText) .filter((streetName) => streetName.includes("de"));
Great video as always, Wes! btw, for #7, "const alpha = people.sort();" will suffice since alphabetical sorting is the default behavior for Array.prototype.sort()
@@reglentless1 Try "console.log(alpha);". You get the exact same output as Wes got. It works because the strings in the people array have the format "LastName, FirstName". If it was "FirstName LastName" then it would sort by FirstName.
Wes actually made a coding error in #7. He should have typed "const [aFirst, aLast] = lastOne.split(", ")" rather than "[aLast, aFirst]". He confused the last and first names. Because he confused these, his code sorts by the first name mistakenly. If you read Wes' output, you can confirm this.
@@brentholcomb8804 In English, "last name" almost always refers to the *family* name, not necessarily the name that appears last. To take an example from the people array - 'Biden, Joseph' - 'Biden' would be the "last name", even though it appears first; and 'Joseph' would be the "first name", even though it appears last.