sum(1)(2)(3)(4)..( n)() | Amazon UI/Frontend Javascript Interview Question

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

КОМЕНТАРІ • 275

  • @cristianouzumaki2455
    @cristianouzumaki2455 5 років тому +68

    you have made videos on such interesting topics that I had to finish them in one sitting . My friends actually thought I was watching some movie. Great vids and explanation especially for a js newbie. keep them coming. I want that bell icon to ring a lot. :)

  • @CristianDatu
    @CristianDatu 4 роки тому +39

    tip: input sum(1)(2)(0)(3)(4)(5)() and watch it blow up ... use typeof to check the existence of b, because 0 is falsy and it won't return a function to be called in the next step, resulting in
    "TypeError: sum(...)(...)(...) is not a function"
    const sum = (a) => (b) => (typeof b === "undefined" ? a : sum(a + b));

  • @humayunkabirdev
    @humayunkabirdev 4 роки тому +54

    This function missing a corner case. If one of the arguments is zero(0) then the functionality will break. we can fix it just by doing a check like *b !== undefined*

  • @laxmangoliya1744
    @laxmangoliya1744 3 роки тому +25

    Just look at the transformation of Akshay between this series and the namaste javascript series. How much he has changed 🔥.

  • @sundar_panda
    @sundar_panda 4 роки тому +5

    That's an awesome video. I guess your solution will return a function.
    Eg: consol.log(sum(2)(3)(4)) => return a function and prints the same.
    To print the sum we can use:
    const addAll = sum(2)(3)(4);
    console.log(addAll());

    • @aldesi
      @aldesi 4 роки тому +3

      It returns a function if you do this
      console.log(sum(1)(2)(3)(4)(5));
      But when you pass an empty argument in the same line at the end it will give you the correct output, like this:
      console.log(sum(1)(2)(3)(4)(5)());

  • @nirmesh44
    @nirmesh44 4 роки тому +1

    Man you are really very honestly giving all the knowledge with all the innocence. Really appreciate your effort.

  • @divyajain1188
    @divyajain1188 4 роки тому +7

    Hey Akshay, Your way of explaining things make everything very simple. The most interseting part is your way of linking everything with simple examples. Can you make one video over Promise, obeservables and async/await?

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

    I saw the question and tried the code myself.. This is what i came up with after 10mins of struggle:
    let sum=(x=null)=>{
    if(x===null)
    return 0
    let acc=x
    return function f(y=null){
    if(y===null)
    return acc
    acc+=y
    return f
    }
    }
    sum(10)(20)(30)() //60
    sum() //0
    sum(0)(0)() //0
    i was so happy that it works.. Then saw your simple beautiful solution and now i am depressed

  • @ozzyfromspace
    @ozzyfromspace 2 роки тому

    Before watching the video and seeing your solution, I tried it as an exercise. Here's my answer:
    function weirdSum(a) {
    if(typeof a === 'number') {
    return function (b) {
    if(typeof b === 'number') return weirdSum(a + b);
    else return a;
    }
    } return a;
    }
    It's more verbose, but I think it's pretty clear. I love how recursion and closures are playing so well together here.

  • @prasadbisure
    @prasadbisure 4 роки тому +3

    I have been asked the same question in interview...i was searching the solution on internet. not found anywhere but .....got here....Thanks a lot Akshay...

  • @parnabsanyal5750
    @parnabsanyal5750 5 років тому +1

    Hi Akshay, I did it with a different approach, but of course your solution is cleaner.
    function sum(val) {
    if (val === void 0) {
    if (sum.previousValues)
    sum.previousValues.push(val)
    else
    sum.previousValues = [val]
    return sum;
    } else {
    const previousValues = sum.previousValues || [] // for handling sum() scenario
    sum.previousValues = null
    return previousValues.reduce((acc, elem) => acc + elem, 0)
    }
    }

  • @krishnachaitanya8088
    @krishnachaitanya8088 3 роки тому +3

    I was not able to solve it in an interview and was not short listed for next round, thanks for explanation

  • @vineetkumar5298
    @vineetkumar5298 4 роки тому +3

    Hey Akshay, thank you for providing a good youtube video on Frontend Javascript interview question. I modify your code to remove the error ocuring in case of sum(10)(10)(0)(40)(). so the solution is
    let sum = a => b => (b || (b==0)) ? sum(a+b) : a;
    console.log(sum(10)(10)(0)(40)());

    • @reidyoung298
      @reidyoung298 3 роки тому

      Could also modify it to include passing in strings, null values, etc. Clarifying questions are important before you start writing code :)

  • @danielpalanki3129
    @danielpalanki3129 5 років тому +6

    The code requires a little bit of adjustment. If one of the arguments in the middle is zero then it will result in an error because the inner function returns the overall result, then attempts to call a function on a non fuction expression. I'm not sure if this problem is mentioned in the video and intentionally excluded because I just quickly checked the final code. :)
    This would I think fix the issue: const add = a => b => !isNaN(b) ? add(a + b) : a

    • @akshaymarch7
      @akshaymarch7  5 років тому +6

      Good catch Daniel. But the main objective of the video was to explain how to attempt this question and and walkthrough the solution by recursively calling the functions. Just a generic approach, the function here could be add, multiply or xyz and corner cases for each will be different. But yeah, I second you. We should add validations for the arguments in all the cases to handle such corner cases :)

    • @pranaysharma7119
      @pranaysharma7119 5 років тому +1

      @@akshaymarch7 you can replace the if(b) condition with if(arguments.length) to solve this error

    • @akshaymarch7
      @akshaymarch7  5 років тому +1

      @@pranaysharma7119 Yes, you're right.

    • @thirdreplicator
      @thirdreplicator 5 років тому

      You should check b if it is undefined. This will fix the 0 argument problem ... var sum = (a) => (b) => b == undefined ? a : sum(a + b)

  • @bharatgora2485
    @bharatgora2485 5 років тому +7

    If I call your sum function like this sum(1)(3)(0)(2)(), it fails. Basically calling your function with parameter 0 causes error. To rectify this issue you need to write your if statement inside the nested function like this. if(b != undefined) or if(arguments.length>0). Second one will not obviously work with arrow functions.

    • @akshaymarch7
      @akshaymarch7  5 років тому +10

      Yes, I got a similar comment earlier also, Bharat.
      But the main objective of the video was to explain how to attempt this question and and walkthrough the solution by recursively calling the functions. Just a generic approach, the function here could be add, multiply or xyz and corner cases for each will be different. But yeah, I second you. We should add validations for the arguments in all the cases to handle such corner cases :)
      Again, Thanks for pointing out. Next time I'll try to cover these small things as well.

  • @nikhil6842
    @nikhil6842 3 роки тому +1

    was asked this same question in recent interview, which ofcourse I nailed thanks to you Akshay

  • @ajayr.n4368
    @ajayr.n4368 3 роки тому

    Thanks to you akshay, trying all these questions out to learn better.
    generic way to do the above for any 2 arg function
    Function.prototype.curry = function(){
    const funcToCurry = this;
    return function curr(a){
    return (b)=> b !== undefined ? curr(funcToCurry(a,b)):a;
    }
    }
    function sum(a,b){return a+b;}
    var curriedSum = sum.curry();
    console.log(curriedSum(4)(5)(3)(8)());

  • @shikhapandey1982
    @shikhapandey1982 2 роки тому

    I was confused with the function,but now am happy.

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

    9:21 why if we do sum(1)(2) and the next argument (3) is reffering to b in sum(a, b)? why a is not the argument(3) in the sum(a,b) after sum(1)(2)(3)?

  • @devmrin
    @devmrin 4 роки тому

    Right solution for the first problem where sum(a)(b)() gives us a+b
    function sum(a){
    return function(b){
    return function() {
    return a + b
    }
    }
    }
    @akshay love your videos.

  • @gauravpandey543
    @gauravpandey543 3 роки тому

    best java script teacher

  • @nirajkumar11111
    @nirajkumar11111 5 років тому +2

    Simply awesome.... just love it as your explanation

  • @prabhjotkalra7479
    @prabhjotkalra7479 4 роки тому +12

    Hey Akshay, can you please upload a video explaining Generator function in detail. This is being used at many places and I am not confident on it.

    • @avinanrey
      @avinanrey 4 роки тому +2

      Veer, I think this one is the best article about Generator function: codeburst.io/understanding-generators-in-es6-javascript-with-examples-6728834016d5
      Message me if you think you need a more in-depth understanding.

  • @giorgimerabishvili8194
    @giorgimerabishvili8194 3 роки тому +7

    That's called "Currying" in JS.

  • @fundiaries__1939
    @fundiaries__1939 4 роки тому +1

    U explain in such an amazing way that js becomes really easy to understand..before watching your videos , I w asjust learning but not enjoying....Now it's been 3 hours I am learning while watching your videos and seems like I am not studying now but just enjoying every bit of the learning time... thanks alot...keep doing this good work...excited to see more related to react and node.js as well...god bless you 😊

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

    Thank you for the clear explanation! It was so helpful to see different variations too!

  • @TheUltimateTrainJourney
    @TheUltimateTrainJourney 4 роки тому +8

    When I used to go for interview in 2012 to 2014 I always wonder why I never encountered such situation while coding for projects 😀

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

    for python developer :
    😁😁😁😁
    def f(a):
    def f(b=False):
    if b:
    return sum(a+b)
    return a
    return f
    sum=f
    print((sum(1)(2)(3)(10)()))
    😅😅😅😅😅😅

  • @Spyrie
    @Spyrie 2 роки тому

    Thank you so much dude. Everything is clear to me. Your videos make my foundation in JS better.

  • @ritsk4338
    @ritsk4338 5 років тому +1

    wow... you made my day... great explanation on recursive function. Thankyou

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

    another implementation
    var sum = function(x, acc){
    if(acc == undefined) acc = x;
    return function(y){
    if(y == undefined) return acc;
    return sum(y, acc + y);
    }
    }

  • @ankitagarwal4914
    @ankitagarwal4914 4 роки тому

    Thank You Akshay! Your videos are very important and best for Frontend Engineering positions

  • @AdnanAli-cp9hk
    @AdnanAli-cp9hk 6 місяців тому

    thankyou so much Akshay Sir, Amazing Amazing explanation of recursion!!

  • @gdebojyoti
    @gdebojyoti 21 день тому

    The shortest answer:
    const sum = x => y => y === undefined ? x : sum(x+y)

  • @ansaanam
    @ansaanam 3 роки тому

    Kal hi inetrview ab pura din ye channel k videos dekhne hai ... Ek sath me 😜 , anyways you are doing good job. Thanks

  • @KASANITEJ
    @KASANITEJ 3 роки тому

    for python developers this can be achieved like this:
    class SUM:
    val = 0
    def __init__(self, val):
    self.val += val
    def __call__(self, val):
    self.val += val
    return self
    def __str__(self):
    return str(self.val)
    print(SUM(1)(2)(3)(4)(5)(6)) #21

  • @Chetan.Kothari
    @Chetan.Kothari 3 роки тому +1

    Thank you for a wonderful explanation😊 I am updating my js skills your videos are helping a lot😊✌👍

  • @deepakporwal8881
    @deepakporwal8881 4 роки тому

    The way you explain is awesome

  • @saddamhussain3431
    @saddamhussain3431 2 роки тому

    Very Awesome Explanation. Thank you

  • @chandankoley7069
    @chandankoley7069 3 роки тому

    I think under the arrow function you don't need a return statement instead of that simply right; let sum = a => b => b ? sum(a+b) : a; console.log(sum(4)(5)(2)());

  • @rajendrakumar729
    @rajendrakumar729 3 роки тому

    Hi Akshay you are explaining complex topics in a simple way ,thanks a lot for that :)

  • @tanugoyal1
    @tanugoyal1 4 роки тому +2

    Thanks a lot Akshay, for all your efforts in making these videos it has helped me and many others to crack the interviews. I am really grateful to you. 🙏😊

    • @akshaymarch7
      @akshaymarch7  4 роки тому +1

      That's great. Congratulations! 😇

    • @tanugoyal1
      @tanugoyal1 4 роки тому

      @@akshaymarch7 Thanks a lot :)

  • @NizamUddin-hh8ub
    @NizamUddin-hh8ub 2 роки тому

    Just awesome ! Keep going ...

  • @mishrarahul
    @mishrarahul 4 роки тому

    beauty of recursion

  • @s.a.Tawhid
    @s.a.Tawhid 3 роки тому

    Sir can you please create a detailed video with a lot of example of currying function, please? I know you have already a video on the topic and I watched that. But somehow I feel like you can make it awesome.

  • @vijaypr4032
    @vijaypr4032 5 років тому

    Wow, that was a 1 line code , Liked it :) (y).
    This was the one which I arrived at :
    function sum(currentTotal) {
    if(!currentTotal) return 0;
    return function(num) {
    if(!num) return currentTotal;
    return sum(currentTotal + num);
    }
    }

  • @razapatel1847
    @razapatel1847 4 роки тому +1

    Plz made video on angular Interview questions for UI developer

  • @ryaneballar125
    @ryaneballar125 5 років тому +5

    To support variadric functions:
    const add = (...a) => (...b) => b.length ? add(...a, ...b) : a.reduce((a, b) => a + b, 0);
    add(1,2)(3,4)(5)(); // 15
    Using an empty parameter invocation as a terminator seems a little bit janky in practical usage. It's probably great to override the returning functions valueOf() as a way to generate the value.
    Although a bit lengthy:
    const add = (...a) => {
    const $add = (...b) => {
    a = a.concat(b);
    return $add;
    }
    $add.valueOf = () => a.reduce((x,y) => x + y, 0);
    return $add;
    }
    +add(1)(2,3)(4,5); //15
    Note the usage of the unary '+' operator as a means to get the primitive value of the function.
    source: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf

    • @akshaymarch7
      @akshaymarch7  5 років тому +1

      Thanks a lot, Ryan. That's a great contribution, it deserves to be pinned on the top!

    • @jayprakashdatani9405
      @jayprakashdatani9405 5 років тому

      I am learning a lot about js now. Yups . Thanks Akshay for being my inspiration

  • @deeptibhagwat7324
    @deeptibhagwat7324 3 роки тому +1

    Hello, I really liked the video, you explained in detail. Can you also explain how instead of sum which we do inside the function , how we can apply the function which was passed as an argument

  • @alexeyshaykov
    @alexeyshaykov 4 роки тому +1

    nice description, thx.
    but how about situations, when call simple sum(1,5) or call sum(1)(2) without () on the end? maybe change function, witch can pass thus case

  • @mateshwari33
    @mateshwari33 2 роки тому

    great explanation ..thank you

  • @singhkumardev
    @singhkumardev 5 років тому +2

    very good job Akshay....please keep it continue ...it is very helpful.....
    Can you please give an understanding for my question...
    I was asked a this question when I was hunting job, that was balance the string with braces for an example "{a}[b]{c:[c]}" if a kind of braces start it should properly close.

    • @akshaymarch7
      @akshaymarch7  5 років тому +1

      Yeah, sure. This is also a very common question asked in the interviews. I've myself faced it. Give me some time I'll surely come up with that. 😊
      And glad that you liked the video, your compliments make me work more hard and come up with something better everytime to help everyone. Thanks. 😊

    • @singhkumardev
      @singhkumardev 5 років тому

      @@akshaymarch7 You are doing a very good job ... and it will help for job seekers like me ;)

  • @lewisflude
    @lewisflude 5 років тому +5

    Really well explained!

    • @Anand-zg6jv
      @Anand-zg6jv 3 роки тому

      Can u tell me the salary of a front end developer in a product base company like Microsoft ,Adobe ,LinkedIn

  • @sankhojjalchatterjee
    @sankhojjalchatterjee 4 роки тому +10

    I was asked this same question in an interview with PwC.

    • @Anand-zg6jv
      @Anand-zg6jv 3 роки тому

      Can u tell me the salary of a front end developer in a product base company like Microsoft ,Adobe ,LinkedIn

    • @Sportgameww
      @Sportgameww Місяць тому

      ​@@Anand-zg6jv15 to 20 LPA.

  • @itscodebot
    @itscodebot 5 років тому +1

    @Akshay Saini, you are doing awesome job. can you make video on how to use immutable.js to structure data in our applications.

  • @adityabhanu9784
    @adityabhanu9784 4 роки тому +3

    I was asked this question when I was interviewing with one of the companies in Delhi.

    • @Anand-zg6jv
      @Anand-zg6jv 3 роки тому

      Can u tell me the salary of a front end developer in a product base company like Microsoft ,Adobe ,LinkedIn

  • @prashantrai4698
    @prashantrai4698 4 роки тому

    Thanks Akshay bro.. I didn't get in first time but got it in second time.

  • @sriram6736
    @sriram6736 3 роки тому

    Nice explanation

  • @himanshushekhar3694
    @himanshushekhar3694 4 роки тому

    awesome video and explaination

  • @niteshkhandelwal2121
    @niteshkhandelwal2121 3 роки тому +4

    Akshay in this example it will return correct output for sum(1)(2)(3)(4)(). But how we will write the code for this sum(1)(2)(3)(4) and output should be 10. I tried and I am getting function as return value. Can we do that or not?

    • @kunal28889
      @kunal28889 3 роки тому

      curry = (fn, length) => function curried(...args) { return length != args.length ? curried.bind(null, ...args) : fn.apply(null, args); };
      add = (a,b)=>a+b;
      finalAdd = curry(add, 2);
      finalAdd(1)(2); //=> 3

  • @lovegoluthemost
    @lovegoluthemost 3 роки тому

    Very helpful to learn

  • @uraniumreza
    @uraniumreza 5 років тому +1

    const sum = (a) => (b) => b ? sum(a+b) : a;
    Thank you mate :)

  • @neerajpathania261
    @neerajpathania261 5 років тому

    Sir, awesome please keep solving and put some questions. So anyone can tey to solve also.

  • @pavanpatil8261
    @pavanpatil8261 3 роки тому

    Hi Akshay... I just got an offer as fresher from a company in Bangalore and they asked this question, I was not able to answer but I answered other questions, I explained concepts as you did, they were impressed. your videos helped me a lot to reach there. Thanks a lot. and I would like to dedicate my first salary to a few of my UA-cam gurus, and you also one among them. kindly let me know how can I support you.

  • @bidsouravbest
    @bidsouravbest 14 днів тому

    you re genius.

  • @elizabethr5161
    @elizabethr5161 3 роки тому

    I was asked this question in an interview today and my bad I wasn't able to crack it as I didn't come across this video before. Hope I'll crack it in future interviews.

  • @itzsarim
    @itzsarim 5 років тому +1

    Hi Akshay,
    Thanks for making this video. I was wondering if you would classify this solution as an example of 'currying' ?

  • @aashishshrivastava9833
    @aashishshrivastava9833 5 років тому

    Hi Akshay I saw your all video are awesome. For js concept as well as interview both .

  • @A1996ARP2075
    @A1996ARP2075 3 роки тому +4

    I am unlucky to miss this video, I have been asked in two interviews till now

  • @SpencerDavis2000
    @SpencerDavis2000 4 роки тому

    thanks. great solution and explanation.

  • @SivaKumar-yh8kn
    @SivaKumar-yh8kn 3 роки тому

    Very helpful ❤️

  • @gyanendra_chaubey
    @gyanendra_chaubey 5 років тому +1

    Awesome questions! Keep up the good work whenever you get time :-)

  • @AmanRaj-zo7bx
    @AmanRaj-zo7bx 2 роки тому

    So finally I am able to fix my keyboard now after watching this video :-)

  • @avinanrey
    @avinanrey 4 роки тому +2

    The following question you'll be getting after this at Amazon is performance. Learn about Memoization as well.

    • @kk7530
      @kk7530 4 роки тому

      @avtar nanrey. I have interview scheduled with Amazon. Can you please provide more tips?

  • @ajaynayak7846
    @ajaynayak7846 4 роки тому

    We can also use an arrow function and take all the argument and pass it one by one, for ex: const fun = (a)=>(b)=> {
    // Code here
    }
    Correct me if I'm wrong

  • @deeraj3069
    @deeraj3069 5 років тому +1

    Thaks for the video.
    Can you make a video on, how this works in function() and arrow function

    • @srujanashankar9517
      @srujanashankar9517 4 роки тому

      const sum = a => (b) => {
      if (b) return sum(a + b);
      return a;
      };
      console.log(sum(1)(2)());

  • @pritamchowdhury4191
    @pritamchowdhury4191 4 роки тому

    Hi Akash,first of all i would like to thank for kind of knowledge you are sharing through your videos.i have faced question about web page optimization in many interview,can you make good video on that.it will be really good knowledge to share i think.

  • @nithishnaidu5542
    @nithishnaidu5542 15 днів тому

    function sum(num, total = 0) {
    if(num === undefined) {
    return total
    }
    return (newNum) => sum(newNum, total + num)
    }
    This is what I implemented before checking the solution.

  • @abhijeetyadav1647
    @abhijeetyadav1647 5 років тому +6

    Hi Akshay.
    Being a front end developer, I've faced similar questions while on job hunt.
    Your videos are extremely informative and helpful and I am sure it'd definitely help job seekers out there.
    Keep up the good work. I hope to see more contents.
    I've a small request for you. A video on javascript design patterns and how different libraries use them under the hood might come in handy somedays.

    • @akshaymarch7
      @akshaymarch7  5 років тому +1

      Sure, Abhijeet. I've noted it down, will try to cover it. It will be a great value add. Thanks for your suggestion :)

    • @Anand-zg6jv
      @Anand-zg6jv 3 роки тому

      Can u tell me the salary of a front end developer in a product base company like Microsoft ,Adobe ,LinkedIn

  • @NickySauce
    @NickySauce 3 роки тому

    Love you bro, ❤️🙏🏻 faadu vid

  • @reshmaramesh1172
    @reshmaramesh1172 2 роки тому

    Let sum = a=> b=> return b? Sum(a+b) : a will have an error. I believe we need to put the return statement in {} or else we can completely omit the return. It will still work.

  • @harmandeepsinghkalsi329
    @harmandeepsinghkalsi329 5 років тому +8

    Hi Akshay , This was good and detailed information on sum(1)(2)(3)() . I have few queries , suppose if the last() is not given and we are still asked to return sum , how can we achieve it ?

    • @satyaarthchhabra4485
      @satyaarthchhabra4485 4 роки тому

      for that case also the same logic will be applied

    • @aldesi
      @aldesi 4 роки тому

      if the last() is not given and we are still asked to return sum, assign the function output to a variable and console log it
      const addAll = sum(2)(3)(4);
      console.log(addAll());

    • @arjuntamble8008
      @arjuntamble8008 3 роки тому

      I've made use of debounce here :)
      const sum = (function(){
      let total = 0;
      let timer;
      function display(total){
      clearTimeout(timer);
      timer = setTimeout(function(){
      console.log(total);
      }, 0);
      }
      return function(num){
      total += num;
      display(total);
      return sum;
      }
      })();

    • @khurshed780
      @khurshed780 3 роки тому

      @@aldesi this is not working

    • @brijspy
      @brijspy 2 роки тому

      @@aldesi its the same buddy afterall u are invoking the function by not passing any arguments

  • @shalinivlogofficial429
    @shalinivlogofficial429 2 роки тому

    Nice bro 👌👌👌👌👌

  • @SenthilKumar-ok2qp
    @SenthilKumar-ok2qp 3 роки тому

    super machi..

  • @sajid2035
    @sajid2035 5 років тому +1

    Hey nice to see ur video buddy.. Good luck..

  • @mohityadav21
    @mohityadav21 3 роки тому

    const sum = (a) => (a ? (b) => (b ? sum(a + b) : a) : 0);
    This also handles `sum()` case.

  • @abhisheksaini2583
    @abhisheksaini2583 3 роки тому

    Thanks man, really helpful

  • @vaishalijain2653
    @vaishalijain2653 4 роки тому

    good explanation bro (y)

  • @avinashrock3538
    @avinashrock3538 4 роки тому

    hi akshay , i love watching your videos , they are really informative , i have a request , can you make a video on asynchronous js , and important features in CSS

  • @ief4545
    @ief4545 4 роки тому

    Hi Akash, Wen through your videos. really helpful. You have stopped adding videos.Any reason?

  • @kiranbabuetukiri2859
    @kiranbabuetukiri2859 3 роки тому

    your videos awesome.

  • @chat-24
    @chat-24 3 роки тому +1

    Hi akshay i am sure u have faced many frontend interview, i just wanted to know have u ever been asked system design question specific to frontend if yes can you please make some video on that

  • @ajitgupta1009
    @ajitgupta1009 4 роки тому +1

    let arr=[1,2,8,2,1,3,6,7];
    let obj={};
    for(let i of arr){
    obj[i]=true;
    }
    console.log(obj);
    let b=Object.keys(obj);
    console.log(b);
    the above code is to remove duplicate from array, i didn't understand the flow inside the for loop. pls can u explain.

    • @ashishsingh-ty6kf
      @ashishsingh-ty6kf 4 роки тому +1

      here obj is working as a HashMap
      support 1 1 2 2 3 3 are your elements
      so obj is basically counting the occurrence of each element and then for the unique element you are just printing the keys of the hashMap i.e obj
      after for loop you obj will be like
      {
      1:2,
      2:2,
      3:3
      }
      see above keys are 1,2,3 and following colon are their occurrences in array and its pretty clear that keys will always give unique elements
      Hope you understood it

  • @sdmj730
    @sdmj730 4 роки тому

    Subscribed....I'm your subscriber now

  • @RahulGupta-go8oe
    @RahulGupta-go8oe 3 роки тому +1

    love you Akshay Saini,

  • @vaddinenisrinivas
    @vaddinenisrinivas 4 роки тому

    Nice bro...

  • @anuraggupta4555
    @anuraggupta4555 2 роки тому

    Hello Akshay! I wrote this solution.
    let sum = num1 => num2 => num3 => num4 => console.log(num4 + num3 + num2 + num1)

    • @anuraggupta4555
      @anuraggupta4555 2 роки тому

      Though your solution is much more efficient!

  • @mayur461
    @mayur461 4 роки тому

    @akshay: there is a variant of this question in which we have to write a function which will give same output to these two invocations
    1) sum(1)(2)(3)....()
    2) sum(1,2,3....)
    Can you make a video for how to approach this question?
    Thanks in advance

    • @brijspy
      @brijspy 3 роки тому

      function sum(...a) {
      if (a.length === 1) {
      a = a[0];
      } else {
      return a.reduce((acc, item) => acc + item, 0);
      }
      return (b) => {
      if (b == undefined) return a;
      return sum(a + b);
      };
      }
      console.log(sum(1)(0)(2)(0)(3)()); // 6
      console.log(sum(1, 2, 3, 4, 5, 6)); // 21

  • @invisiblevlogger7017
    @invisiblevlogger7017 4 роки тому +1

    3:10 sum(1)(2)() = 3
    var sum = function (a){
    return function(b){
    return a + b;
    }
    }
    it will not return 3 but a error : VM473:6 Uncaught TypeError: sum(...)(...) is not a function

  • @jashanpreet832
    @jashanpreet832 3 роки тому

    Good