Тёмный

JavaScript Array Cardio Practice - Day 1 -  

Wes Bos
Подписаться 178 тыс.
Просмотров 244 тыс.
50% 1

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

Наука

Опубликовано:

 

1 янв 2017

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 500   
@66lucc
@66lucc 2 года назад
The Odin Project we grinding!
@seimataniguchi8654
@seimataniguchi8654 23 дня назад
Yup.
@ginnerzapata5909
@ginnerzapata5909 4 года назад
1) .filter(): 1:17 1.1) console.table(): 2:48 2) .map(): 3:55 3) .sort(): 5:16 4) .reduce(): 7:47 **Did this 'cause I come back constantly.
@bennewton8954
@bennewton8954 Год назад
2 years later but I totally missed the console.table until reading this comment 🤝
@ginnerzapata5909
@ginnerzapata5909 Год назад
Wow! And today I’m software engineer and UI designer. Thanks a lot wesbos! For you guys who are starting: you will succeed!
@gbbarn
@gbbarn 2 месяца назад
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!
@trophydevice7624
@trophydevice7624 3 года назад
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.
@joshhillmedia
@joshhillmedia 3 года назад
thank you!
@mish2149
@mish2149 3 года назад
thanks!
@elijahayandokun8080
@elijahayandokun8080 3 года назад
Exactly! He has 7 entries and I (we) have 12 entries.
@jdragon8184
@jdragon8184 2 года назад
Dont worry about the results, worry about the stuff
@MaxDamageTV
@MaxDamageTV 2 года назад
Phew. Cheers
@WhichKoreaIsBetter
@WhichKoreaIsBetter Год назад
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.
@yehiazaglol6170
@yehiazaglol6170 Год назад
thanks bro
@raulizquierdopujante2680
@raulizquierdopujante2680 Год назад
thanks will do the same!
@aderonkelawal6452
@aderonkelawal6452 Год назад
can you please share the link to some documentation? thank you
@dkmn3385
@dkmn3385 Год назад
@@aderonkelawal6452 just search for the functions and open the MDN articles
@Al_gre
@Al_gre Год назад
specially for reduce method and how to solve 8 exercise - it was nightmare.
@dbzmm1
@dbzmm1 2 года назад
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.
@marcofernstaedt8179
@marcofernstaedt8179 Год назад
same here im working on the Odin Project. although the problem with the link wont allow me to do the task eve when i copy and past the code
@shivakharbanda8376
@shivakharbanda8376 Год назад
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!!!!!!!!!!!!!!!!!!
@2222melissa
@2222melissa Год назад
@@marcofernstaedt8179 I had the same issue. I think my issue was from the "links .map .filter" part of the code.
@gustavovidal5267
@gustavovidal5267 Год назад
Amazing project, here you have the name theodinproject GIYF
@fidellivino
@fidellivino Год назад
I come from TOP too... How are you doing now?
@josedanielrodriguez3348
@josedanielrodriguez3348 Год назад
5 years, and this video still invaluable, best content of javascript to learn array methods
@WesBos
@WesBos Год назад
That’s the beauty of JS fundamentals
@CoryTheSimmons
@CoryTheSimmons 6 лет назад
console.table!! Nice!!
@kidd8393
@kidd8393 4 года назад
This was the gem of the week for me.
@kennethornieta2375
@kennethornieta2375 Год назад
true hahaha
@Fuckingcoward
@Fuckingcoward Год назад
congrats if you have made it this far in The Odin Project, a lot of people quit here as it gets harder keep going champ!
@armansiddiqi8036
@armansiddiqi8036 Год назад
thanks buddy hoping that you had completed the course
@youtubejproasters1224
@youtubejproasters1224 11 месяцев назад
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 ?
@jimmyjimenez8599
@jimmyjimenez8599 11 месяцев назад
@@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
@chimpankie1954
@chimpankie1954 8 месяцев назад
@@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 :)
@jimmyjimenez8599
@jimmyjimenez8599 8 месяцев назад
@@chimpankie1954definitely should, I just finished my todo project not long ago and now I’m learning Asynchronous code.
@Sagwafu
@Sagwafu 4 года назад
I LOOOOOVE seeing you tighten up/reduce the size of the code. That is so freaking helpful.
@fidgetspinner343
@fidgetspinner343 5 лет назад
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.
@MVIVN
@MVIVN Год назад
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!
@ThePolaris87
@ThePolaris87 7 лет назад
These array cardio videos as a FANTASTIC IDEA! What a great concept to improve on the fundamentals! Would love to see more videos likes this!
@mohamedbaha-eddinebouzerou3269
you can solve more algorithms chalenges and sure you gonna reach his level
@avinash8584
@avinash8584 2 года назад
@@mohamedbaha-eddinebouzerou3269 Where?
@sunjungwoo
@sunjungwoo 2 года назад
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);
@onux16
@onux16 Год назад
obligatory DRY remark, that you could/should use a pure function to find an age e.g. getAge = (person) => person.passed - person.year;
@c.0pium
@c.0pium Год назад
returning 1 and -1 is kinda confusing to me yet i know how they works
@carolinaserrano8267
@carolinaserrano8267 11 месяцев назад
@@c.0pium could you explain what are the meanings of 1 and -1? I just cant understand why returning them orders the array
@justalonelypoteto
@justalonelypoteto 5 месяцев назад
@@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
@lenaryan
@lenaryan 5 лет назад
"Bennington, Chester" [starts crying]
@Madesh-qf6qn
@Madesh-qf6qn Месяц назад
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.
@stevenwatkin9844
@stevenwatkin9844 3 года назад
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.
@espensteen8406
@espensteen8406 Год назад
Yeah, I was going to comment this as well. simply callig .sort() with no parameters does the job for this scenario.
@wxmachinegun
@wxmachinegun Год назад
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.
@sadlersol
@sadlersol Год назад
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???
@justalonelypoteto
@justalonelypoteto 5 месяцев назад
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
@JordanAF808
@JordanAF808 3 года назад
this is just what I needed! Your way of explaining these subjects is great!
@Symuality
@Symuality 3 года назад
As a beginner, I really hope to be on your level one day. You look like a Dev God from where I am sitting right now lol.
@ZakvonMammel
@ZakvonMammel 2 года назад
where are you at now?
@Symuality
@Symuality 2 года назад
@@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
@ZakvonMammel 2 года назад
@@Symuality That's awesome to hear! reduce gives me some issues as well, so I think you're good there. Do you still see him as a Dev God?
@Symuality
@Symuality 2 года назад
@@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!
@ZakvonMammel
@ZakvonMammel 2 года назад
@@Symuality Thanks for the replies! I bid you good, clean code on your journey :)
@adriendm
@adriendm 2 года назад
The one working with information from Wikipedia was absolutely great. I felt nice doing it. Thank you
@francisco.t.a.g.
@francisco.t.a.g. 3 года назад
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!
@cheekianteoh5746
@cheekianteoh5746 Год назад
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
@danbuild977
@danbuild977 6 лет назад
This challenge is just what I have been looking for!
@ruddyriverocabero8157
@ruddyriverocabero8157 4 года назад
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.
@hongquannguyen5532
@hongquannguyen5532 6 месяцев назад
true
@eddybayes5160
@eddybayes5160 2 года назад
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! :)
@rotrose7531
@rotrose7531 4 года назад
Your are Great and recommended by Steve Stiffith, most formidable javascript teacher. Thank you for doing all these for us.
@DragonWhisky
@DragonWhisky 2 года назад
Thank you, Wes Bos. These practices help a lot in my learning journey.
@solidstatejake
@solidstatejake 5 лет назад
Thanks for your work, Wes. I'm learning a lot.
@liliponds
@liliponds 2 месяца назад
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.)
@CronosXIIII
@CronosXIIII 4 месяца назад
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.
@LagrangePoint0
@LagrangePoint0 3 месяца назад
Hey friend, could you explain to me how did he accessed the DOM from wikipedia on his editor?.
@sejoonpark6200
@sejoonpark6200 3 месяца назад
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.
@samdeleon8121
@samdeleon8121 3 года назад
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!
@oisterzenitzer9748
@oisterzenitzer9748 Год назад
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!😁
@dean4763
@dean4763 Год назад
Wow, Another seriously underrated JS tutorial. I also came here via Odin Project. Liked and subscribed. Thanks Wes Bos.
@user-jq3sz4hg6k
@user-jq3sz4hg6k 5 лет назад
Love you Bos. You are the greatest teacher I've ever met. Truly. We appreciate your ability to spark the interest. Best regards!
@user-jq3sz4hg6k
@user-jq3sz4hg6k 5 лет назад
I fond of the way you showing how to do things different ways also.
@mikesalcedo5321
@mikesalcedo5321 Год назад
Awesome video incredibly helpful !! Currently doing the Odin Project and this was just incredible!
@MsLeeAS
@MsLeeAS 6 лет назад
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
@azrael9986
@azrael9986 4 года назад
const inventors = [ { first: "Albert", last: "Einstein", year: 1879, passed: 1955 }, { first: "Isaac", last: "Newton", year: 1643, passed: 1727 }, { first: "Galileo", last: "Galilei", year: 1564, passed: 1642 }, { first: "Marie", last: "Curie", year: 1867, passed: 1934 }, { first: "Johanne", last: "Keppler", year: 1571, passed: 1630 }, { first: "Nicolous", last: "Corpenicus", year: 1473, passed: 1543}, { first: "Max", last: "Planck", year: 1858, passed: 1947}, ];
@sugihwarascom
@sugihwarascom 3 года назад
thx dude
@navbravic1355
@navbravic1355 Год назад
7:11 "ternary" is actually a simpler word than you're thinking. It's just Turn-Ary. Very similar to canary (the bird).
@ahmednabikhan6883
@ahmednabikhan6883 Год назад
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 )
@deveren
@deveren Год назад
thank you so much,, love ur video series!
@sakshigosain238
@sakshigosain238 Год назад
this was the next level content I needed for my javascript skills
@WesBos
@WesBos Год назад
glad to hear it!
@umut_satir
@umut_satir Год назад
I totally understand your videos, your speaking is very clear. Coming from TheOdinProject. Thank you so much for the content
@DineshPatil-gn4cz
@DineshPatil-gn4cz 5 лет назад
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.
@Hacking-NASSA-with-HTML
@Hacking-NASSA-with-HTML 3 года назад
So hot examples! Thanks, Wes Bos! ​👍 ✅
@wolfderechter3020
@wolfderechter3020 4 года назад
Well explained and great video, thank you!
@willmaple
@willmaple Год назад
the was awesome you're so efficient with your keystrokes my fingers were hurting a little trying to keep up without pausing too much.
@TheAluminus
@TheAluminus 7 лет назад
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
@Admlass Год назад
Yep you need to use the spread syntax to avoid mutating the original array, e.g [...people]
@ByronWWW
@ByronWWW Год назад
@@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?
@TheLopsidedobject
@TheLopsidedobject 3 года назад
For #7, it still works if you remove everything within the sort method.
@emin6626
@emin6626 Год назад
Maybe we should have sorted the first names.
@bedribulut
@bedribulut Год назад
excellent and informative practice. thank you so much mate.
@ozoneri
@ozoneri 2 года назад
This was very useful. Thank you so much!
@joshieval
@joshieval 2 месяца назад
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);
@rodrigoreynaga4013
@rodrigoreynaga4013 3 года назад
Thanks, man. Great job !!
@Tyler1986
@Tyler1986 4 года назад
People is already sorted by last name. Even if they weren't, people.sort(); would return what you wanted without any other work.
@watchyoutb
@watchyoutb 3 года назад
was thinking the same about simply writing people.sort()
@DenKuznets_
@DenKuznets_ Год назад
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 ]++
@lorefort
@lorefort Год назад
That was really good, thanks!
@BobbyBundlez
@BobbyBundlez 4 года назад
man.. just came back to this for review and that last one hit a new kinda way this time.. PSHH so helpful
@depressito
@depressito Год назад
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.
@Bwhaahhhb
@Bwhaahhhb 6 лет назад
How to say ternary: turn-air-e. Thanks for the vids! : )
@01107345
@01107345 3 года назад
More like turn-uh-ree
@jean-francoisbouzereau6258
@jean-francoisbouzereau6258 4 года назад
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
@PaYaw-jj5mf
@PaYaw-jj5mf Год назад
hey jean, i am a beginner wrapping my head around .reduce() and obj[item] use case. I'd like an alternative explanation if possible. Possibly a call?
@user-ct5oo3do7b
@user-ct5oo3do7b 3 года назад
"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
@momoxiaoshizi 6 лет назад
i have a major crush on your code. I thought I already understood these methods, but I never wrote them the way you do.
@zacremboldt9160
@zacremboldt9160 6 лет назад
@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.
@krzemian
@krzemian 4 месяца назад
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.
@moola123
@moola123 2 года назад
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
@Evangelos_Bl
@Evangelos_Bl Год назад
Thanks for the help!!!
@merlin3784
@merlin3784 12 дней назад
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.
@martywallace8784
@martywallace8784 2 года назад
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.
@gabrielmedeiros6886
@gabrielmedeiros6886 2 года назад
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 )
@ericyantis8084
@ericyantis8084 2 года назад
@@gabrielmedeiros6886 or just... const alpha = people.sort(); // This sorts it all because the array starts with the last name.
@gabrielmedeiros6886
@gabrielmedeiros6886 2 года назад
@@ericyantis8084 LOL yeah that too
@joshuahinchey7544
@joshuahinchey7544 2 года назад
@@ericyantis8084 I came here for this.
@ByronWWW
@ByronWWW Год назад
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).
@fragrantbloom
@fragrantbloom 2 года назад
Great video! Thanks :)
@BrennanKill
@BrennanKill 2 года назад
You inspired me to look up the pronunciation of Ternary operator: ter-ner-e
@serhendiefendi
@serhendiefendi 6 лет назад
Great tutorials
@kahuna2588
@kahuna2588 Год назад
for sort function you can also do: ((a,b) => {return a.year - b.year})
@0ako
@0ako 3 года назад
Ternary operator: ter-nar-y, 3 syllables lol. Thanks for the video
@TheAznBeaner
@TheAznBeaner 8 месяцев назад
6 years old and still a great cardio exercise
@gazisalahuddin8681
@gazisalahuddin8681 5 лет назад
thank you..pls keep it up !
@unpluggedaman
@unpluggedaman 2 года назад
Thanks a lot Wes
@WILLGYS
@WILLGYS 2 года назад
That was good cardio!
@BOURGEOISMORGAN
@BOURGEOISMORGAN 5 месяцев назад
TOP thank you !
@walfaenger8385
@walfaenger8385 4 дня назад
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.
@mathws1
@mathws1 5 лет назад
thanks Bos, you're the man!
@ByronWWW
@ByronWWW Год назад
The inventors object in the exercises contains 11 inventors now, looks like it was 7 when this was filmed.
@EyadAlkhalidy
@EyadAlkhalidy 9 месяцев назад
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).
@brianofoblivion
@brianofoblivion 3 года назад
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"
@eddiehead6964
@eddiehead6964 Год назад
i dont understand how he can get access to all the links from an external page using DOM around minute 12:00 can someone explain?
@shreyasrajagopal4433
@shreyasrajagopal4433 4 месяца назад
the querySelectorAll('a') loads all the links into the links variable.
@gusk1777
@gusk1777 Год назад
10:09 Tripped out for a moment as I almost fell asleep just before he said this.
@bavs8a
@bavs8a 6 лет назад
Holy gold mine
@davidborden3181
@davidborden3181 2 года назад
with #6 i'm getting errors saying querySelector is 'null.' Im confused as to how you can grab anything from a link that's commented out?
@Sometextpodcast
@Sometextpodcast 4 месяца назад
I'm facing the same issue
@leonelgauna1174
@leonelgauna1174 4 года назад
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
@nathancornwell1455
@nathancornwell1455 4 года назад
for sorting oldest to youngest you could just do this: ordered = inventors.sort((a,b) => a.year - b.year; works just the same.
@dephunkius5885
@dephunkius5885 Год назад
I don't know if the pun was intended at 9:42, but it made me laugh
@anagreug
@anagreug 6 лет назад
hahaha, i did actually go for a drink. great practice!
@jamesbrooks9321
@jamesbrooks9321 5 лет назад
why is the result of my reduce function 861 and yours is 523
@user-jq3sz4hg6k
@user-jq3sz4hg6k 5 лет назад
Don't worry, your result is correct. On the video Bos is using a little bit another array.
@11am
@11am 4 года назад
cool not only me
@gaddiction9610
@gaddiction9610 4 года назад
your answer is correct .the data in his array is limited
@salthesadmanshark5645
@salthesadmanshark5645 3 года назад
mine too
@baguette7869
@baguette7869 3 года назад
mine too lol
@mathiasrjacobsen
@mathiasrjacobsen 5 лет назад
Holy shit Wes you are on fire :D
@AshishKumar-rq8gb
@AshishKumar-rq8gb 4 года назад
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
@1lucas132
@1lucas132 4 года назад
Thanks Wes
@sebaba001
@sebaba001 2 года назад
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.
@kazumakiryu8185
@kazumakiryu8185 2 года назад
incredible
@jakobfrederikson5808
@jakobfrederikson5808 2 года назад
A for loop reads much nicer to me than the reduce function
@sakhamenmhlongo281
@sakhamenmhlongo281 4 года назад
const ordered = inventors.sort((a,b) => (a.year - b.year)) also works.
@Birandoo
@Birandoo 3 месяца назад
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"));
@SpaYco
@SpaYco 4 года назад
18:06 reading the names made me laugh so much idk why xD
@Richard_Stroker
@Richard_Stroker 2 года назад
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
@reglentless1 2 года назад
This won't sort by last name though mate, i dont think?
@Richard_Stroker
@Richard_Stroker 2 года назад
@@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.
@brentholcomb8804
@brentholcomb8804 2 года назад
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.
@Richard_Stroker
@Richard_Stroker 2 года назад
@@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.
@Ludo045
@Ludo045 2 года назад
but, you need to imagine if you need to sort by surname..
@user-lz6rj5zd4u
@user-lz6rj5zd4u 5 месяцев назад
Odin project gang
Далее
JavaScript Array Mastery: Tips, Tricks & Best Practices
1:02:49
когда повзрослела // EVA mash
00:40
Просмотров 2,4 млн
8 Must Know JavaScript Array Methods
10:05
Просмотров 1 млн
How To Think Like A Programmer
1:00:07
Просмотров 2 млн
Learn JavaScript Array Reduce In 10 Minutes
10:22
Просмотров 197 тыс.
What is THIS in JavaScript? 👈
4:27
Просмотров 13 тыс.
JavaScript Pro Tips - Code This, NOT That
12:37
Просмотров 2,5 млн
JavaScript Higher Order Functions & Arrays
34:56
Просмотров 979 тыс.
SSD с кулером и скоростью 1 ГБ/с
0:47
Mac Studio из Китая 😈
0:34
Просмотров 130 тыс.
Gizli Apple Watch Özelliği😱
0:14
Просмотров 4,3 млн