JavaScript reduce() method in 5 minutes! ♻

// .reduce() = reduce the elements of an array
// to a single value
// ---------- EXAMPLE 1 ----------
const prices = [5, 30, 10, 25, 15, 20];
const total = prices.reduce(sum);
console.log(`$${total.toFixed(2)}`);
function sum(accumulator, element){
return accumulator + element;
}
// ---------- EXAMPLE 2 ----------
const scores = [75, 50, 90, 80, 65, 95];
const maximum = scores.reduce(getMax);
const minimum = scores.reduce(getMin);
console.log(maximum);
console.log(minimum);
function getMax(accumulator, element){
return Math.max(accumulator, element);
}
function getMin(accumulator, element){
return Math.min(accumulator, element);
}

Пікірлер: 30

  • @BroCodez
    @BroCodez7 ай бұрын

    // .reduce() = reduce the elements of an array // to a single value // ----------- EXAMPLE 1 ----------- const prices = [5, 30, 10, 25, 15, 20]; const total = prices.reduce(sum); console.log(`$${total.toFixed(2)}`); function sum(accumulator, element){ return accumulator + element; } // ----------- EXAMPLE 2 ----------- const scores = [75, 50, 90, 80, 65, 95]; const maximum = scores.reduce(getMax); const minimum = scores.reduce(getMin); console.log(maximum); console.log(minimum); function getMax(accumulator, element){ return Math.max(accumulator, element); } function getMin(accumulator, element){ return Math.min(accumulator, element); }

  • @user-mk2md9pu5m

    @user-mk2md9pu5m

    7 ай бұрын

    Django course please

  • @Praeda19
    @Praeda193 ай бұрын

    Dude, thank you SO much, the way you've explained how the three data transformation array methods work, as well as the how the forEach loop works, is incredibly easy to understand. I've finally got my head around how the forEach loop works, and now JS is (ifnally) starting to click with me. Again, thank you, and all the best!!

  • @javohir1704

    @javohir1704

    Ай бұрын

    just restart

  • @geldelian
    @geldelian5 ай бұрын

    You said that the first iteration would be sum(0, 5), but that is not true. If you don't set the initial value for reduce the first value will be taken as 1 element of the array and the first iteration will be sum(5, 30).

  • @RafaelSilva-rv6kt

    @RafaelSilva-rv6kt

    3 ай бұрын

    Yeah, if you think about it, the minimum value would be 0 if that was the case, and not 50

  • @iluvx360

    @iluvx360

    14 күн бұрын

    Good catch

  • @RayhanAsif22
    @RayhanAsif222 ай бұрын

    Dude I've learned a lot from your videos !!

  • @Mochinori99
    @Mochinori9923 күн бұрын

    This video is absolutely brilliant and so so clear. I love your videos. Thank you.

  • @zrotrasukha8733
    @zrotrasukha87334 ай бұрын

    Thank you brother, you helped me a lot, May god bless you !!

  • @pini5076
    @pini50763 ай бұрын

    thank you so much!

  • @magomihaly1741
    @magomihaly17417 ай бұрын

    You re the man thx bro

  • @islamabdelhakiim7258
    @islamabdelhakiim7258Ай бұрын

    thank you so much

  • @rukecodes
    @rukecodes7 ай бұрын

    Thanks bro!!!!!!

  • @kathikr9360
    @kathikr93604 ай бұрын

    thank you dude

  • @manitamao6564
    @manitamao65643 ай бұрын

    Thank u bro

  • @nomanbangtan4120
    @nomanbangtan41207 ай бұрын

    Bro you should do a project video.

  • @kathikr9360
    @kathikr93602 ай бұрын

    thanks again

  • @pexhay4690
    @pexhay46907 ай бұрын

    is there any difference between defining the callback that way and using arrow notation inside reduce like prices.reduce((accum, el) =>{...})

  • @ianfrye8988

    @ianfrye8988

    7 ай бұрын

    For the most part no, just depends on if you’re going to reuse that function… when you get into the “this” keyword it does depending on the context

  • @pexhay4690

    @pexhay4690

    7 ай бұрын

    @@ianfrye8988 thank you

  • @greengraphics5060
    @greengraphics50605 ай бұрын

    Do a node js video

  • @devl0ver666
    @devl0ver66627 күн бұрын

    now i know why your channel name is bro code ................. :)

  • @javohir1704
    @javohir1704Ай бұрын

    would be a way better if you added a initialValue parameter but thank you though

  • @cyberblitz
    @cyberblitzАй бұрын

    shame you didn't include the initial value parameter too. Otherwise, very educational.

  • @allhailalona
    @allhailalona15 күн бұрын

    good video, but what about objects? u forgot those.

  • @user-mk2md9pu5m
    @user-mk2md9pu5m7 ай бұрын

    django course please

  • @AdamGassouma
    @AdamGassouma8 күн бұрын

    I did this : let username = ["Mr","Adam","Gassouma"]; let full = username.reduce(fullname); console.log(full); function fullname(previous,next){ return previous+" " + next; }

  • @ianfrye8988
    @ianfrye89887 ай бұрын

    Also all other JavaScript higher order array methods can be created with reduce