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!
@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 😊
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
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!
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 😊
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.
@@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 🥳
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?
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 :)
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++ }
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?
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 :)
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
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
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.
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.
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 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 😉
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!
@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 😊
Best JavaScript playlist on earth🥺
A second bonus question? Oh wow, this was unheard of! Looking forward to the next exercises
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
@@TechWithNader Yeah, I actually joined two days ago, such a chill server
Feel free to keep commenting here on specific videos as you go through or you can spam me in there too, haha 😂
Bonus exercise 2 was indeed challenging 😵💫
great way to explain....i would like to start solving codility exercises and explain your though rocess while you solve them
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!
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 😊
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.
@@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 🥳
Thank you!
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?
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 :)
@@TechWithNader STRINGIFIED. If you don't make that a T-shirt, then why are we even doing this?
@@JoeMilneEnglish Lmao, merch incoming 👕
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++
}
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?
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 :)
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
@@TechWithNader Thanks :)
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);
}
});
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
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
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.
@@TechWithNader Good point. Thank you!
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.
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) 🤓
@@TechWithNader Nice one, man. Loving it! What is Big O? 😬
@@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 😉