8 Javascript Solutions You HAVE TO KNOW

Поделиться
HTML-код
  • Опубликовано: 25 окт 2024

Комментарии • 46

  • @DaveGrayTeachesCode
    @DaveGrayTeachesCode  3 года назад +1

    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

    • @1conscience0dimension
      @1conscience0dimension Год назад

      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.

  • @bohdanmukha9317
    @bohdanmukha9317 3 года назад +6

    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.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 года назад +7

      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. 🙏💯😀

    • @ahmad-murery
      @ahmad-murery 3 года назад +2

      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

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 года назад +2

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

    • @ahmad-murery
      @ahmad-murery 3 года назад +2

      @@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

  • @friendofzeus
    @friendofzeus Месяц назад

    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.

  • @ahmad-murery
    @ahmad-murery 3 года назад

    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

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 года назад +3

      All great alternative solutions! Thanks for contributing Ahmad. 🙏🚀

    • @ahmad-murery
      @ahmad-murery 3 года назад +2

      @@DaveGrayTeachesCode No, Thank you Dave for the great content 💎🥇 ,
      I hope you continue publishing similar JS challenges

  • @MrMadcage
    @MrMadcage 3 года назад +2

    Hi Dave!
    return array.sort((a, b) => b - a);
    It's too good to descending order?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 года назад

      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);

  • @Uncaught_in_promise
    @Uncaught_in_promise 3 года назад +1

    Absolutely precious content. Thanks

  • @soniablanche5672
    @soniablanche5672 3 года назад +2

    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(" ")

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 года назад +1

      Great catch! I use trim() often with inputs, and it sounds like I didn't think about it here. Thanks for the comment 💯🚀

  • @nagaistark
    @nagaistark 2 года назад

    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?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 года назад

      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.

  • @chetanjain4616
    @chetanjain4616 2 года назад

    Hey, great video, you can also do this ```array.filter((a: number | string) => typeof a === "number"``` for filtering through number array

  • @MuhammadAdnan2.0
    @MuhammadAdnan2.0 3 года назад

    Superb ... Next video on Reduce method full examples pls....

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 года назад +1

      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 😀🚀

    • @MuhammadAdnan2.0
      @MuhammadAdnan2.0 3 года назад

      @@DaveGrayTeachesCode I mean full depth video on Reduce and filter method like u did in depth of sort.

  • @jamshidtashkent1976
    @jamshidtashkent1976 Год назад

    Very good solutions

  • @rangabharath4253
    @rangabharath4253 3 года назад

    Awesome as always 👍😀

  • @deeptimonga7375
    @deeptimonga7375 3 года назад

    Useful !!! Thanks!

  • @mortezafarhangpanah256
    @mortezafarhangpanah256 4 месяца назад

    Awesome❤❤

  • @hsardrake5373
    @hsardrake5373 3 года назад

    Good video!

  • @Blooding
    @Blooding 3 года назад

    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.

  • @1conscience0dimension
    @1conscience0dimension Год назад

    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)

  • @hardwired66
    @hardwired66 3 года назад

    love it

  • @RcToysUa
    @RcToysUa 2 года назад

    Like!

  • @thanushanx
    @thanushanx 3 года назад

    Gold

  • @frankmmd1861
    @frankmmd1861 2 года назад

    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 ! :)