This tutorial takes a deeper dive into the JS sort method and common JS problems / questions that you may be asked during an interview. As I note in the video, there is almost always more than one way to solve a problem... and I'm sharing my version. If you have a different solution, please share it below. 🙏😀 Just starting with Javascript? Check out my 8 hour full course tutorial: ruclips.net/video/EfAl9bwzVZk/видео.html
I must add this I had to do in a plugin if (this.plugin.settings.filters === "mostSwitched") { allPluginsList.sort((a, b) => b.switched - a.switched) } else { allPluginsList.sort((a, b) => a.name.localeCompare(b.name)) } first this is sorted with a number and else by string. but they are properties in an object so I still need to use a and b ,and then you need this localeCompare.
One thing most of the online teachers, even the good ones, do not realise / do not want to account for - RUclips is a global platform, so your average viewer can very well not be the native English speaker. So, they talk way, way too fast. That's why I appreciate not only educational side of your videos, but also your pace. It makes your content easy to digest even for those who are beginners not only in coding, but also in English.
Thank you! I have also been criticized for talking slowly - not just on YT, but in general, even when I was young. I know the playback speed can be increased for those who want to go faster. Thank you for appreciating my natural chill and laid-back pace. 🙏💯😀
One of the principles I learned while preparing to become a teacher is to talk in a pace and language/accent that all students in the class will understand, I don't know if Dave has it in his nature or he just learned it, in both cases I think this is a must-have skill (or at least a plus) for any teacher. For me I'm learning Programming as well as English by watching Dave's videos
@@ahmad-murery thank you my friend! I did not realize you were learning English at the same time. You continue to be impressive! I live in Kansas (the middle of the U.S.), and I may say some things differently (or with an accent) compared to other English speakers you hear.
@@DaveGrayTeachesCode As a native Arabic speaker I can't easily distinguish the accent, they all look the same to me 😎😁, I sometimes notice some difference in the pronunciation of some words by different people but it's mostly understandable, so don't worry Thanks Dave
For most common occurring string, a more optimal approach would be to create an object with a single entry and a value. While we're looping, compare the value to the number we're incrementing. If the increment is larger then just replace the entry. Function would then return the key in that object. Does away with the need to create a new hash map and having to sort it.
Hello Dave, I came with some alternatives (as I always like to do) - In numberSortDesc function, We can return b - a in the sort function intstead of reversing the sort result - In sortNumbersAndStrings function, We can use the ternary operator to return the array and then we can push to the result array.forEach(el => { (isNaN(el)? strArray: numArray).push(el) }) Or we can use filter instead of forEach (although I think forEach is faster here), isNaN will consider number-like string as number, so if we want to consider everything in quotes as string we can use the typeof operator const numArray = array.filter(el => typeof el == 'number'); const strArray = array.filter(el => typeof el == 'string'); - In mostCommonWithTies function, we can directy eterate through object properties using Object.entries method Object.entries(countObj).forEach(entry => { sortArray.push(entry); }) - In countAllWords/mostCommonWord functions, wordArray regex can be rewritten like this to clear all but word characters and spaces const wordArray = string.toLowerCase().replace(/[^\w ]/g, '').replace(/\s{2,}/g, ' ').split(' '); Thanks for this great skill builder videos
Hello! Yes, that is the idea for descending order. Try it out like this: let numbers = [4, 2, 5, 1, 3]; numbers.sort((a, b) => b - a); console.log(numbers);
For the "countAllWords" function, you didn't take into account trailing whitespace. example: " a b ".split(" ") returns ["","a","b",""]. This can be fixed by calling .trim() before .split(" ")
Top-notch tutorials! A quick question regarding the use of regex (28:40): Wouldn't it have been easier to run _.replace_ just once with _(/\W+/g, ' ')_ as the parameters? Or is it the greedy nature of the *+* quantifier that you try to avoid here?
It has been too long since I made this to remember but there is usually more than one way with regex ! 💯 Regex is something I seem to have to look up every time.
Thanks for the suggestion. I could do a video on reduce() only. I already have one on higher order functions that includes reduce() as well as forEach(), filter(), and map(): ruclips.net/video/7BeT6lsudL4/видео.html I hope it helps get you started 😀🚀
Sweet. I have not finished the video yet, but I wanted to do an "unexpected" solution for the second topic. ahaha const arrayNumAndString = [1.1, 100000, 21, 30, 4, "zebra", "abc", "medium"] const toString = arrayNumAndString.map(x => x.toString()).sort().map(x => isNaN(x) ? x : Number.isInteger(parseFloat(x)) ? parseInt(x) : parseFloat(x)) console.log(toString)
for some reason code on 18:00 didnt work, my browser couldn't count elements in an array, then I found this way : array.forEach((el) => { countObj[el] = countObj[el] ? (countObj[el] + 1) : 1; }); and it worked ! :)
This tutorial takes a deeper dive into the JS sort method and common JS problems / questions that you may be asked during an interview. As I note in the video, there is almost always more than one way to solve a problem... and I'm sharing my version. If you have a different solution, please share it below. 🙏😀 Just starting with Javascript? Check out my 8 hour full course tutorial: ruclips.net/video/EfAl9bwzVZk/видео.html
I must add this I had to do in a plugin
if (this.plugin.settings.filters === "mostSwitched") {
allPluginsList.sort((a, b) => b.switched - a.switched)
} else {
allPluginsList.sort((a, b) => a.name.localeCompare(b.name))
}
first this is sorted with a number and else by string. but they are properties in an object so I still need to use a and b ,and then you need this localeCompare.
One thing most of the online teachers, even the good ones, do not realise / do not want to account for - RUclips is a global platform, so your average viewer can very well not be the native English speaker. So, they talk way, way too fast. That's why I appreciate not only educational side of your videos, but also your pace. It makes your content easy to digest even for those who are beginners not only in coding, but also in English.
Thank you! I have also been criticized for talking slowly - not just on YT, but in general, even when I was young. I know the playback speed can be increased for those who want to go faster. Thank you for appreciating my natural chill and laid-back pace. 🙏💯😀
One of the principles I learned while preparing to become a teacher is to talk in a pace and language/accent that all students in the class will understand,
I don't know if Dave has it in his nature or he just learned it, in both cases I think this is a must-have skill (or at least a plus) for any teacher.
For me I'm learning Programming as well as English by watching Dave's videos
@@ahmad-murery thank you my friend! I did not realize you were learning English at the same time. You continue to be impressive! I live in Kansas (the middle of the U.S.), and I may say some things differently (or with an accent) compared to other English speakers you hear.
@@DaveGrayTeachesCode As a native Arabic speaker I can't easily distinguish the accent, they all look the same to me 😎😁,
I sometimes notice some difference in the pronunciation of some words by different people but it's mostly understandable, so don't worry
Thanks Dave
For most common occurring string, a more optimal approach would be to create an object with a single entry and a value. While we're looping, compare the value to the number we're incrementing. If the increment is larger then just replace the entry. Function would then return the key in that object. Does away with the need to create a new hash map and having to sort it.
Hello Dave,
I came with some alternatives (as I always like to do)
- In numberSortDesc function,
We can return b - a in the sort function intstead of reversing the sort result
- In sortNumbersAndStrings function,
We can use the ternary operator to return the array and then we can push to the result
array.forEach(el => {
(isNaN(el)? strArray: numArray).push(el)
})
Or we can use filter instead of forEach (although I think forEach is faster here),
isNaN will consider number-like string as number, so if we want to consider everything in quotes as string we can use the typeof operator
const numArray = array.filter(el => typeof el == 'number');
const strArray = array.filter(el => typeof el == 'string');
- In mostCommonWithTies function, we can directy eterate through object properties using Object.entries method
Object.entries(countObj).forEach(entry => {
sortArray.push(entry);
})
- In countAllWords/mostCommonWord functions, wordArray regex can be rewritten like this to clear all but word characters and spaces
const wordArray = string.toLowerCase().replace(/[^\w ]/g, '').replace(/\s{2,}/g, ' ').split(' ');
Thanks for this great skill builder videos
All great alternative solutions! Thanks for contributing Ahmad. 🙏🚀
@@DaveGrayTeachesCode No, Thank you Dave for the great content 💎🥇 ,
I hope you continue publishing similar JS challenges
Hi Dave!
return array.sort((a, b) => b - a);
It's too good to descending order?
Hello! Yes, that is the idea for descending order.
Try it out like this:
let numbers = [4, 2, 5, 1, 3];
numbers.sort((a, b) => b - a);
console.log(numbers);
Absolutely precious content. Thanks
You're welcome!
For the "countAllWords" function, you didn't take into account trailing whitespace.
example: " a b ".split(" ") returns ["","a","b",""]. This can be fixed by calling .trim() before .split(" ")
Great catch! I use trim() often with inputs, and it sounds like I didn't think about it here. Thanks for the comment 💯🚀
Top-notch tutorials!
A quick question regarding the use of regex (28:40):
Wouldn't it have been easier to run _.replace_ just once with _(/\W+/g, ' ')_ as the parameters?
Or is it the greedy nature of the *+* quantifier that you try to avoid here?
It has been too long since I made this to remember but there is usually more than one way with regex ! 💯 Regex is something I seem to have to look up every time.
Hey, great video, you can also do this ```array.filter((a: number | string) => typeof a === "number"``` for filtering through number array
Thanks for sharing!
Superb ... Next video on Reduce method full examples pls....
Thanks for the suggestion. I could do a video on reduce() only. I already have one on higher order functions that includes reduce() as well as forEach(), filter(), and map(): ruclips.net/video/7BeT6lsudL4/видео.html I hope it helps get you started 😀🚀
@@DaveGrayTeachesCode I mean full depth video on Reduce and filter method like u did in depth of sort.
Very good solutions
Thank you!
Awesome as always 👍😀
Thanks again!
Useful !!! Thanks!
Glad to hear that! 🚀💯
Awesome❤❤
Good video!
Thanks! 🙏
There is a site called doseitmutate which is helpful. It's a list of methods and shows if it mutates the original array or not. Also gives examples.
Good to know - thanks for sharing! 🙏💯
Sweet. I have not finished the video yet, but I wanted to do an "unexpected" solution for the second topic. ahaha
const arrayNumAndString = [1.1, 100000, 21, 30, 4, "zebra", "abc", "medium"]
const toString = arrayNumAndString.map(x => x.toString()).sort().map(x => isNaN(x) ? x : Number.isInteger(parseFloat(x)) ? parseInt(x) : parseFloat(x))
console.log(toString)
love it
Thanks! 🙏
Like!
Gold
Thanks! 🙏
for some reason code on 18:00 didnt work, my browser couldn't count elements in an array, then I found this way :
array.forEach((el) => {
countObj[el] = countObj[el] ? (countObj[el] + 1) : 1;
});
and it worked ! :)