JavaScript reduce() method in 5 minutes! ♻️

Поділитися
Вставка
  • Опубліковано 5 вер 2024
  • // .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);
    }

КОМЕНТАРІ • 36

  • @BroCodez
    @BroCodez  9 місяців тому +8

    // .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);
    }

    • @Blitz61wasd
      @Blitz61wasd 9 місяців тому

      Django course please

  • @Praeda19
    @Praeda19 5 місяців тому +8

    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!!

  • @AdamGassouma
    @AdamGassouma 2 місяці тому +2

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

  • @Mochinori99
    @Mochinori99 3 місяці тому +1

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

  • @t.antonyjaneaustus3184
    @t.antonyjaneaustus3184 8 днів тому

    Very good explaination dude..its more help full to me

  • @devl0ver666
    @devl0ver666 3 місяці тому +3

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

  • @cyberblitz
    @cyberblitz 4 місяці тому +1

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

  • @piotrmazgaj
    @piotrmazgaj 24 дні тому

    This is my seal. I have watched the entire video, understood it, and I can explain it in my own words, thus I have gained knowledge. This is my seal.

  • @MikeCode-iy9sb
    @MikeCode-iy9sb 2 місяці тому

    very good explaination. thanksSoMuch~

  • @RayhanAsif22
    @RayhanAsif22 5 місяців тому +1

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

  • @zrotrasukha8733
    @zrotrasukha8733 7 місяців тому

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

  • @constdev_
    @constdev_ 2 місяці тому

    //Using Arrow Function:
    //Arrow Function is awesome :D
    const grades = [70, 65, 75, 89, 94];
    const maximum = grades.reduce((previous, next) => Math.min(previous, next));
    console.log(maximum);

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

    I rarely comment.
    You're awesome!

  • @xxxxxharuxxxxx
    @xxxxxharuxxxxx 12 днів тому

    oh my god you saved my life thank you!!!!!

  • @javohir1704
    @javohir1704 4 місяці тому

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

  • @nomanbangtan4120
    @nomanbangtan4120 9 місяців тому +1

    Bro you should do a project video.

  • @greengraphics5060
    @greengraphics5060 7 місяців тому +1

    Do a node js video

  • @muhammetguloglu6268
    @muhammetguloglu6268 2 місяці тому

    GOD BLESS YOU BRO!

  • @allhailalona
    @allhailalona 3 місяці тому

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

  • @youthpost_34
    @youthpost_34 5 днів тому

    Thanks Brooo👊

  • @pexhay4690
    @pexhay4690 9 місяців тому

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

    • @ianfrye8988
      @ianfrye8988 9 місяців тому

      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 9 місяців тому

      @@ianfrye8988 thank you

  • @pini5076
    @pini5076 6 місяців тому

    thank you so much!

  • @Blitz61wasd
    @Blitz61wasd 9 місяців тому +1

    django course please

  • @islamabdelhakiim7258
    @islamabdelhakiim7258 3 місяці тому

    thank you so much

  • @magomihaly1741
    @magomihaly1741 9 місяців тому

    You re the man thx bro

  • @manitamao6564
    @manitamao6564 5 місяців тому

    Thank u bro

  • @kathikr9360
    @kathikr9360 6 місяців тому

    thank you dude

  • @rukecodes
    @rukecodes 9 місяців тому

    Thanks bro!!!!!!

  • @kathikr9360
    @kathikr9360 5 місяців тому

    thanks again

  • @ianfrye8988
    @ianfrye8988 9 місяців тому

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