I'm a Software Engineer from Hyderabad, India
Side projects, pair programming, discussions :)
You are using the reduce method wrong. The function being provided as an argument must return the value of the operation, but in your case it is console.log() which is why you are getting NaN. This is how you are supposed to use it function multiply(...args){ return args.reduce((a, b) => a * b); }; console.log(multiply(...list)); Explanation The first argument is the accumulator and the second argument is the current value The reduce method passes each element of the array as the second argument, whereas the first argument is initialised with the first value in the array for the first time and the result of the function from next time. I have written the same method in a different way so it's easier to see what's going on under the hood, function multiply(...args){ return args.reduce(function (a,b) { console.log(a + ',' + b); return a*b; }); }; You can run the above method in the browser's developer tools and this is the output you'll get As you can see, first 1, 2 gets logged which is the value of the accumulator and the second value in the array. Now the multiplication happens a*b which results in 1*2=2 which will be sent as the accumulator value for the next time. Next, 2, 3 gets logged since a = 1*2 = 2 and b = 3 (third element in array) Next 6, 4 gets logged since a = 2*3 = 6 and b = 4 (fourth element in array) and so on. Please refer this MDN article for more information.
Hi all, I'm Ravi and I am a Software Engineer at Kenyt.ai. I have been fascinated by programming since my high school, I still remember writing my first program in Turbo C and how excited I was when I finally saw the output, it's a simple addition program (it took me like 2 hours to figure out that I had to compile and execute to provide the inputs :p). Apart from programming, I play video games on my Xbox, go on bicycle rides during weekends and occasionally blog about tech stuff. I love writing in markdown and have been hunting for a blogging platform that supports markdown for quite a while now and almost decided to go with Github Pages + Jekyll, but luckily I found HashNode and it has been great, Kudos to the HashNode team. I work on some silly side projects from time to time (like this snake game), If you have any silly ideas like this, I'd love to collaborate, ping me anytime!) That's all about me. Happy hacking folks!