How to sum an array in javascript using the reduce method #shorts

preview_player
Показать описание
------------

Рекомендации по теме
Комментарии
Автор

Reduce also has a second parameter that you can you use to set an initial value.

razvbir
Автор

That was by far the clearest explanation of the sum method!

Well done!

dancehalllyrics
Автор

Don't know why I'm finding shorts more helpful these days. Your video is just one of them. Thanks!

ebubesunday
Автор

You should add 0 as initial value for sum to prevent any unexpected result

SpaghettiRealm
Автор

Very helpful and way shorter to write than a whole for loop 👌.
Thanks so much for the video 👍!

dcknature
Автор

Start on line 6. It's one more line of ease of visibility

assaulterpt
Автор

Other useful tricks
Standard deviation
let sdev = Math.sqrt(arr.map(v=>v*v).reduce((a, b)=>a+b));

Yutaro-Yoshii
Автор

even shorter: numbers.reduce((sum, value) => sum + value) // return and "{" "}" not needed

repenning
Автор

In Python, you can use sum() function.

SyberiaK
Автор

this kind of looks like about the same amount of code as doing a normal loop, but its imo a lot more descriptive. it does something specialized, and it seems to pretty clearly tell you that specialized thing it's doing, so that definitely is a plus over doing a normal loop for this kind of situation

Templarfreak
Автор

One crucial feature is the initial value. If you don't provide one the first value in your array will be the first sum value, and iteration starts at index 1.

accidentalengineering
Автор

Bruh i love you... you're making my life so simple

nanaowusuoduro
Автор

ALWAYS provide the second initial value.

mrgerbeck
Автор

Suggestion for this Youtube SHORT VIDS, can you somehow Zoom out the result.. cause daammmn.. I cannot see the full result.

MrJeszam
Автор

I don't know the practical use of this method except Sum of the numbers 🤣

swanand
Автор

And that ladies and gentleman, is why undefined + 1 = 1.

danbopes
Автор

I think you missed the default value for .reduce, optional though after the callback.

donaldjr.labajo
Автор

Learning JS for a project after learning a few other languages. I can't help but ask what were they smoking when they came up with this crazy syntax? 😂

disko.kommando
Автор

u forgot to mention one more crucial param of the callback fn. the initial starting value.

ndk
Автор

i know that after the callback function, you have to put 0, to start from 0 or something. to start from index 0 or something, don t know

eduardmarinca