Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there another way to reduce the array to a single number

reduced an array to a single number by multiplication of all numbers, but i get the wrong output

let numbs = [1, 2, 3, 4, 5, 6, 7, 8];

let reducedNumb = numbs.reduce((prevVal, currVal)=>
prevVal * currVal
,0)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

console.log(reducedNumb); //output is 0

>Solution :

Simple math mistake. Something times 0 is always 0 😉

let reducedNumb = numbs.reduce((prevVal, currVal)=> prevVal * currVal ,1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading