Oof! Exercise 2 was killer but after you explained it, I think it really clicked. I was close on my own! /* 1. Create an array called "numbers" with the following values: [10, 20, 15, 30, 15, 20, 35, 60, 10] 2. Find the first duplicate value in "numbers" and print it out to the terminal HINT: Add the numbers to a Set as you for-loop over "numbers" *This is quite challenging and is a common interview question */ // Step 1: const numbers = [10, 20, 15, 30, 15, 20, 35, 60, 10]; // Step 2: // -> "brain storage" const seenNumbers = new Set(); // -> "looking at each item" for (let i = 0; i < numbers.length; i++) { if (seenNumbers.has(numbers[i])) { // found first duplicate console.log(numbers[i]); break; } // -> have not seen it, we need to store it (brain storage) seenNumbers.add(numbers[i]); } // 15
Randomly found out you can add the array elements to a Set and spread set into a new array in one line: const uniquePoints = [...new Set(points)]; So cool!
Yeah I wanted to keep the mess-ups in haha, since everyone gets lost in what's happening and I'm no different ;) I'm glad I caught it and fixed it and didn't realize too late, haha!
Join the discord before it gets too crowdy guys! :) After the second bonus exercise from the Map exercises video wich I struggled with a lot, this second excercise felt more natural when solving! In the exercise 3 I didn' t get the proper solution because in the else statement i put: "duplicates.add(items[i])" instead of "duplicates.add(items[i].name)" So close! Thanks Nader, this is the best guide for JS and it's just a matter of time for TH-cam algorithm to pump it up like it deserves!
Hey Gabriele! Glad you enjoyed these exercises 😃 Looks like you pretty much had the idea with the 3rd one, just with one little extra trick and push to get over the finish line, haha! Keep it up and thanks for the kind words 😊
I had a comment breaking down my thought process and how I broke down these problems in my mind and I REFRESHED THE PAGE BEFORE COMMENTING >.< i'll just redo these problems again tomorrow and write it out again lol, but challenge 2 was very tricky but still appreciated, I console logged duplicate numbers and got back my set instead of console logging numbers[i] to get back the individual duplicated value lol, sometimes I don't understand my own thought process.
on exercise 3 I was staring at this for like 3 hours const items = [ { name: "banana", quantity: 1, price: 1.95 }, { name: "apple", quantity: 1, price: 1.45 }, { name: "banana", quantity: 10, price: 0.05 }, { name: "candy", quantity: 1, price: 3.5 }, ]; results = []; const duplicates = new Set(); for (let i = 0; i < items.length; i++) { if (duplicates.has(items[i].name)) { continue; } else { duplicates.add(items[i].name); } } console.log(results); I think my thought process was I would have to transfer what was being inputted into our duplicate set, and transfer the values of that somehow into results. All I had to do was transfer (i) in the loop, while our set was checking for duplicates. until I watched your solution and it makes alot of sense. Its these small things that get me every time but i'll follow up on exercise 2 and 3 tomorrow and see if I can push through without watching the solution >.
I really appreciate how challenging these exercises are, and how much information we have to recall back from previous lessons. You've honestly been the best teacher I've ever had, and it's crazy to think how much I enjoy learning now vs learning in school and hating it
I'm super proud to hear you say all these things and you documenting your progress and through process as you go. It's so so so important to have that resilience and keep trying until you eventually succeed! You are definitely demonstrating that as you go through this and I can see it building, well done! It's sometimes not all about the code but all these ancillary things you pick up as you go through the process and motions 😊
Hi Nader, for exercise 2, could we argue that using an array instead of a set would yield the same result since we're simply using it as a container to store unique values? Or is there a performance / conciseness benefit? // With Array const duplicatesArr = []; numbers.some(number => { if (!duplicatesArr.includes(number)) { duplicatesArr.push(number); } else { console.log("First duplicate", number); return true; } }); // With Set const duplicatesSet = new Set(); numbers.some(number => { if (duplicatesSet.has(number)) { console.log("First duplicate", number); return true; } else { duplicatesSet.add(number); } });
Just for fun, here's a solution without using a set, by using filter in the for loop instead: for (let i = 0; i < items.length; i++) { const currentItem = items[i]; if (!results.filter((item) => item. name === currentItem.name).length) { results.push(currentItem); } } (The space between `item.` and `name` in the code should be removed, I think TH-cam interpreted that as a link so it wouldn't post my comment.) Filter returns an array with all the objects in the results array that have the same name property as the item we are currently at in our for loop. If the array returned by filter has a length of 0, this means that the object with that name property is not yet in the results array, so the code inside the if block is run.
I absolutely love your teaching style and how you simplify the material without making me feel stupid. Thank you so much and keep up the great videos.
oh man! I love your video so much! I am grateful to you.
Best JavaScript playlist on earth🥺
Thank you for such insightful videos! You put time and effort to carefully craft each lesson.
Oof! Exercise 2 was killer but after you explained it, I think it really clicked. I was close on my own!
/*
1. Create an array called "numbers" with the following values:
[10, 20, 15, 30, 15, 20, 35, 60, 10]
2. Find the first duplicate value in "numbers" and print it out to the terminal
HINT: Add the numbers to a Set as you for-loop over "numbers"
*This is quite challenging and is a common interview question
*/
// Step 1:
const numbers = [10, 20, 15, 30, 15, 20, 35, 60, 10];
// Step 2:
// -> "brain storage"
const seenNumbers = new Set();
// -> "looking at each item"
for (let i = 0; i < numbers.length; i++) {
if (seenNumbers.has(numbers[i])) {
// found first duplicate
console.log(numbers[i]);
break;
}
// -> have not seen it, we need to store it (brain storage)
seenNumbers.add(numbers[i]);
}
// 15
Nice work! Glad it sticks after that! The fact you were close is amazing as this was challenging too. I'm glad the "brain" idea helps :D
Randomly found out you can add the array elements to a Set and spread set into a new array in one line:
const uniquePoints = [...new Set(points)];
So cool!
Loved that you had a little mess-up on exercise-3. I questioned it when you did it, but glad you went back and fixed it with me! Haha.
Yeah I wanted to keep the mess-ups in haha, since everyone gets lost in what's happening and I'm no different ;) I'm glad I caught it and fixed it and didn't realize too late, haha!
Join the discord before it gets too crowdy guys! :)
After the second bonus exercise from the Map exercises video wich I struggled with a lot, this second excercise felt more natural when solving!
In the exercise 3 I didn' t get the proper solution because in the else statement i put:
"duplicates.add(items[i])"
instead of
"duplicates.add(items[i].name)"
So close! Thanks Nader, this is the best guide for JS and it's just a matter of time for TH-cam algorithm to pump it up like it deserves!
Hey Gabriele! Glad you enjoyed these exercises 😃 Looks like you pretty much had the idea with the 3rd one, just with one little extra trick and push to get over the finish line, haha! Keep it up and thanks for the kind words 😊
I had a comment breaking down my thought process and how I broke down these problems in my mind and I REFRESHED THE PAGE BEFORE COMMENTING >.< i'll just redo these problems again tomorrow and write it out again lol, but challenge 2 was very tricky but still appreciated, I console logged duplicate numbers and got back my set instead of console logging numbers[i] to get back the individual duplicated value lol, sometimes I don't understand my own thought process.
on exercise 3 I was staring at this for like 3 hours
const items = [
{ name: "banana", quantity: 1, price: 1.95 },
{ name: "apple", quantity: 1, price: 1.45 },
{ name: "banana", quantity: 10, price: 0.05 },
{ name: "candy", quantity: 1, price: 3.5 },
];
results = [];
const duplicates = new Set();
for (let i = 0; i < items.length; i++) {
if (duplicates.has(items[i].name)) {
continue;
} else {
duplicates.add(items[i].name);
}
}
console.log(results);
I think my thought process was I would have to transfer what was being inputted into our duplicate set, and transfer the values of that somehow into results. All I had to do was transfer (i) in the loop, while our set was checking for duplicates.
until I watched your solution and it makes alot of sense. Its these small things that get me every time but i'll follow up on exercise 2 and 3 tomorrow and see if I can push through without watching the solution >.
I really appreciate how challenging these exercises are, and how much information we have to recall back from previous lessons. You've honestly been the best teacher I've ever had, and it's crazy to think how much I enjoy learning now vs learning in school and hating it
I'm super proud to hear you say all these things and you documenting your progress and through process as you go. It's so so so important to have that resilience and keep trying until you eventually succeed! You are definitely demonstrating that as you go through this and I can see it building, well done! It's sometimes not all about the code but all these ancillary things you pick up as you go through the process and motions 😊
Hi Nader, for exercise 2, could we argue that using an array instead of a set would yield the same result since we're simply using it as a container to store unique values? Or is there a performance / conciseness benefit?
// With Array
const duplicatesArr = [];
numbers.some(number => {
if (!duplicatesArr.includes(number)) {
duplicatesArr.push(number);
} else {
console.log("First duplicate", number);
return true;
}
});
// With Set
const duplicatesSet = new Set();
numbers.some(number => {
if (duplicatesSet.has(number)) {
console.log("First duplicate", number);
return true;
} else {
duplicatesSet.add(number);
}
});
Just for fun, here's a solution without using a set, by using filter in the for loop instead:
for (let i = 0; i < items.length; i++) {
const currentItem = items[i];
if (!results.filter((item) => item. name === currentItem.name).length) {
results.push(currentItem);
}
}
(The space between `item.` and `name` in the code should be removed, I think TH-cam interpreted that as a link so it wouldn't post my comment.)
Filter returns an array with all the objects in the results array that have the same name property as the item we are currently at in our for loop. If the array returned by filter has a length of 0, this means that the object with that name property is not yet in the results array, so the code inside the if block is run.
Good
Hi
🚀
This is first time I got something from my brain,
ex-2 : my approach
const firstDuplicate = new Set();
for(let i = 0; i