Exercises: Maps - Javascript In Depth

Поділитися
Вставка
  • Опубліковано 18 лис 2024

КОМЕНТАРІ • 36

  • @TechWithNader
    @TechWithNader  Рік тому +10

    If bonus-exercise-2 was too difficult for you, don't worry! We will do more of this in the next set of exercises for Sets - no pun intended. Keep going!

    • @TechWithNader
      @TechWithNader  Рік тому +1

      @Bombastik_Adam Awesome! That’s the spirit and definitely the way to go! Even if you didn’t get it right away you definitely learned a lot by trying and will be able to solve more next time and next time after that 😊

  • @nanlunglongtau2076
    @nanlunglongtau2076 8 місяців тому +2

    Best JavaScript playlist on earth🥺

  • @siancalebdomasig5067
    @siancalebdomasig5067 Рік тому +5

    A second bonus question? Oh wow, this was unheard of! Looking forward to the next exercises

    • @TechWithNader
      @TechWithNader  Рік тому +2

      Haha! I sometimes get a little TOO excited 😂
      By the way, I don't know if I sent you the invite link at all for Discord if you're interested: discord.gg/K4nkugP7Gd

    • @siancalebdomasig5067
      @siancalebdomasig5067 Рік тому +2

      @@TechWithNader Yeah, I actually joined two days ago, such a chill server

    • @TechWithNader
      @TechWithNader  Рік тому +2

      Feel free to keep commenting here on specific videos as you go through or you can spam me in there too, haha 😂

  • @OXIDE777-is6gs
    @OXIDE777-is6gs Рік тому +2

    Bonus exercise 2 was indeed challenging 😵‍💫

  • @tt2318
    @tt2318 Рік тому +1

    great way to explain....i would like to start solving codility exercises and explain your though rocess while you solve them

  • @KRAKENBACK..
    @KRAKENBACK.. Рік тому +4

    oh lord i already know im gonna have to rewatch bonus exercise 2 for the next week to solidify that concept and understanding into my mind haha Thank you for the challenge tho, its crazy seeing how you break apart the problem and find a solution!

    • @TechWithNader
      @TechWithNader  Рік тому +2

      Haha, these are getting quite challenging so that's to be expected! Keep in mind it's also unfair to compare to me since I've been doing this way longer - so it's more a way to see how you'd break it apart and work towards that over time as you gain more experience working through projects of your own 😊

    • @KRAKENBACK..
      @KRAKENBACK.. Рік тому +3

      Hello Nader this is my Day 2 of solving exercise 2, I'm going into this without watching your solution and I'm going to write my though process out.
      First we add the numbers array, and create a seenNumbers map to keep track of the numbers[i], and the [i](indecies) and create a for loop to loop through the numbers array.
      Secondly, we create an if statement for SeenNumbers if we've seen the numbers[i] (duplicate), but we haven't seen anything yet so lets jump to the else.
      Third) We are going to want to set our map so we can keep track of the numbers in the array, so we will -> seenNumbers.set(numbers[i], i) (it will create a key value property inside our seenNumbers Map everytime SeenNumbers doesn't have numbers[i] as one of the keys)
      4th) Now lets jump back up to the first if statement and console.log something, hmmm we are going to want to console log the first duplicate index that we see so lets log (i), and then we are going to want to log the index of the first pair of that duplicate so (seenNumbers.get(numbers[i]), which is the value of seenNumbers(Numbers[i]).
      5th) now we write console.log(i, seenNumbers.get(numbers[i]) under the first ifStatement if seenNumbers.has(numbers[i]). AND WE'RE DONE!!!!!!!
      I think the hardest part for me was utilizing the map properties in a way that I could recall keys and values that we need for the solution, at first I would just get overwhelmed and would lose myself in the thought process of breaking down the problem because I knew the tools were there, I just didn't know how to use them, but after seeing you use your problem solving skills with the tools in front of you it really inspired me to try by myself today. Thank you Nader.

    • @TechWithNader
      @TechWithNader  Рік тому +2

      @@KRAKENBACK.. Wow, amazing work! It really was a tough one and even just go through that explanation process you wrote down is so key to understanding most things in development. Well done! You'll notice that the more you do it, the more types of problems you can break down and solve as you learn more tools as well 🥳

  • @senniagordinskaya4051
    @senniagordinskaya4051 5 місяців тому

    Thank you!

  • @JoeMilneEnglish
    @JoeMilneEnglish 2 роки тому +3

    Very interesting. Is it fair to say that in an Object, a Key with the same name as another Key will overwrite the first Key, regardless of whether they seem to have different types (i.e. string "1" and number 1)? Whereas in a Map, different types can play the role of Key (i.e. string "1" and number 1 won't overwrite one another), right?

    • @TechWithNader
      @TechWithNader  2 роки тому +2

      Yup, exactly, nice! An Object has ONLY string keys, so anything that's not a string will get "stringified" and used as the key. A Map does not do this and uses the actual thing itself as the key, regardless of what it is :)

    • @JoeMilneEnglish
      @JoeMilneEnglish 2 роки тому +2

      @@TechWithNader STRINGIFIED. If you don't make that a T-shirt, then why are we even doing this?

    • @TechWithNader
      @TechWithNader  2 роки тому +1

      @@JoeMilneEnglish Lmao, merch incoming 👕

  • @user-wy6iy7ij1z
    @user-wy6iy7ij1z Рік тому +1

    it isn't the most inteligent method, but it works. I didn't use map, just for loop and a buch of variable.
    const numbers = [60,11,15,30,15,20,35,60,10]
    let duplicate
    let index
    let first = 1
    for(let i = 0; i < numbers.length; i++){
    for(let x = first; x < numbers.length; x++){
    if(numbers[i] == numbers[x]){
    duplicate = i
    index = x
    break
    }
    }
    if(index){
    break
    }
    first++
    }

  • @Elahehgoodarzi
    @Elahehgoodarzi 10 місяців тому +1

    Hello Nader,
    Thank you for the video and exercise, I have one issue with Bonus-exercise1
    If I dont consider log key as string I will get an error
    myConsole.set(log, (msg) => console.log(msg));
    myConsole.get(log)("Hello");
    I got this error: Reference Error log is not defined,
    Why is happening?

  • @zornicadimitrova2863
    @zornicadimitrova2863 Рік тому +2

    Hello, Nader ! :) I have question.We create new Map() with 6 entryes ,but If we have 2000 entryes how to create them?Line by line ..example: nameOfVariable.set("some key","some value") 2000 times.I'm interesting what is the other way? You teach so well. It is my pleasure to learn from you. Have a nice day :)

    • @TechWithNader
      @TechWithNader  Рік тому

      Hey Zornica! Great question! In that case, we can actually provide the entries to the Map constructor: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/Map#creating_a_new_map

    • @zornicadimitrova2863
      @zornicadimitrova2863 Рік тому

      @@TechWithNader Thanks :)

  • @emacsultron6212
    @emacsultron6212 11 місяців тому +1

    My solution for bonus 2
    const numbers = [10, 20, 15, 30, 15, 20, 35, 60, 10];
    const seenNumbers = new Map();
    numbers.some((number, index) => {
    if (seenNumbers.has(number)) {
    console.log(index, seenNumbers.get(number));
    return true;
    } else {
    seenNumbers.set(number, index);
    }
    });

  • @rovic2hacking505
    @rovic2hacking505 Рік тому +1

    console.log(store.get("products")[store.get("products").indexOf(candy)]) this might be the replacement for for loop when accesing the index of the object in an array of a map

  • @JoeMilneEnglish
    @JoeMilneEnglish 2 роки тому +3

    For step 5 in exercise 3, I got:
    console.log(
    houseForSale.get("offers").reduce((prev, curr) => {
    if (prev > curr) {
    return prev;
    }
    return curr;
    }, 0)
    );
    // 315000

    • @TechWithNader
      @TechWithNader  2 роки тому +2

      Nice work! It's awesome to see you get practice with this and use it here :) I would get in the habit of naming the prev and curr what they are as much as you can too, as it really helps when you come back to look at the code.

    • @JoeMilneEnglish
      @JoeMilneEnglish 2 роки тому +1

      @@TechWithNader Good point. Thank you!

  • @JoeMilneEnglish
    @JoeMilneEnglish 2 роки тому +3

    Right. I've got it written down and saved in GitHub (BONUS-exercise-2.js) but my goal tomorrow is to go over it and UNDERSTAND it, hahaha. Wow. What in the world is going on there. I'll figure it out:
    const numbers = [10, 20, 15, 30, 15, 20, 35, 60, 10];
    const seenNumbers = new Map();
    for (let i = 0; i < numbers.length; i++) {
    if (seenNumbers.has(numbers[i])) {
    console.log(i, seenNumbers.get(numbers[i]));
    } else {
    seenNumbers.set(numbers[i], i);
    }
    }
    // 4 2
    // 5 1
    // 8 0
    MINDBLOWING.

    • @TechWithNader
      @TechWithNader  2 роки тому +3

      I couldn't help but include the second bonus, so I'm glad it's blowing some minds haha! It truly is magical though once you get it, but take your time to as this is something that would come up in an interview to really put you over and above (when talking about Big O or efficiency) 🤓

    • @JoeMilneEnglish
      @JoeMilneEnglish 2 роки тому +2

      @@TechWithNader Nice one, man. Loving it! What is Big O? 😬

    • @TechWithNader
      @TechWithNader  2 роки тому +2

      @@JoeMilneEnglish It’s an analytical framework for thinking about how fast or slow things are relative to each other (in programming). So I can objectively say that my function is way cooler than yours 😉