Good catch. The reason that works in this case is our function is only called with integers, so JavaScript implicitly converts the array into a string, e.g. '[1, 2, 3]' -> '1, 2, 3'. But if you try doing that with an array of objects, it will be implicitly converted to this: ''[object Object],[object Object],[object Object']' Obviously, in that case the key would not be unique. JSON.stringify() prevents this bug, because it gives us the most precise string representation of the array/objects.
nit:- The Leetcode problem number in the title says 2629 but in fact is 2623. Might make it difficult for people searching for a solution by the problem number. Also, thanks for the daily uploads. They make us all better programmers each day - one bit at a time :)
I am an experienced JS developer (yes experienced for job tasks i mean 😂) but this is great for logic building. Thanks a lot 🫰 keep uploading. Also can you please share your website graph config? I want to take a look at it. It uses the antv x6 graph right?
i used an array as the key here and it worked fine
Good catch. The reason that works in this case is our function is only called with integers, so JavaScript implicitly converts the array into a string, e.g. '[1, 2, 3]' -> '1, 2, 3'.
But if you try doing that with an array of objects, it will be implicitly converted to this: ''[object Object],[object Object],[object Object']'
Obviously, in that case the key would not be unique. JSON.stringify() prevents this bug, because it gives us the most precise string representation of the array/objects.
@@NeetCodeIO thanks for the clarification
@@NeetCodeIO thanks!
bro please help to study hard and understand the js concept more ,where I can study the concept in js
Your videos are so helpful ! thank you from bottom of my heart!!
Glad I could help =)
very nicely explained, thankyou so much.
great explanation, thank you
I used *args.join("-")* as the key which worked for this problem with integers.
But *JSON.stringify()* is a better option.
THANKS!
nit:- The Leetcode problem number in the title says 2629 but in fact is 2623. Might make it difficult for people searching for a solution by the problem number. Also, thanks for the daily uploads. They make us all better programmers each day - one bit at a time :)
My b, fixed!
thanks man for great explanation .
thankyou so much it helps me alot
2:22 like! I do not now same about React, because u now
I am an experienced JS developer (yes experienced for job tasks i mean 😂) but this is great for logic building. Thanks a lot 🫰 keep uploading.
Also can you please share your website graph config? I want to take a look at it. It uses the antv x6 graph right?
Day 9 of doing the 30-day challenge with NeetCode
in the stringify function, why do we use args, and not the spread operator ...args?
because args act as a single element but ...args act as multiple elements
let's say we memoized a function sum, should memoize(1,2,3) and memoize(3,2,1) be considered the same and the callCount be 1?
Ideally not, it's not always the same fn that is passed as a parameter to memoize function
2:24 a fun moment 😆
Thank you so much
I see that on line 12 you have cache[key] = fn(...args);
My question is, if cache is declared as a const, how are you able to add to it?
I have answered my own question, using const simply means you cannot reassign the variable name. You CAN still mutate it.
I have learnt frm your answer to your question . Thak you 😂😂😂😂
const as in non changeable only applies to primitives and not for non primitive objects like arrays
Did you solve memorize 2 ?
I didnt find it :(
Amazing
Taking about the design pattern,
Isn't this strategy design pattern?
Because we could pass any parameter(strategy) to memoize.
what is difference between
a)
const key = args.toString();
and
b)
const key = JSON.stringify(args);
?
why b) is faster than a) by 2-3 times always???
We can re convert to JSON object using JSON.parse().
2:25 if you are writing angular, why don't you go for typescript in daily js challenge? XD
keep it up
doesnt work ... . I get fn proto
What if order of arguments is different but values are same
Different key obviously can't use cached value