JavaScript reduce() method in 5 minutes! ♻️

Поделиться
HTML-код
  • Опубликовано: 10 ноя 2023
  • // .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);
    }

Комментарии • 31

  • @BroCodez
    @BroCodez  8 месяцев назад +6

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

  • @Praeda19
    @Praeda19 4 месяца назад +5

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

  • @Mochinori99
    @Mochinori99 2 месяца назад

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

  • @RayhanAsif22
    @RayhanAsif22 3 месяца назад +1

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

  • @zrotrasukha8733
    @zrotrasukha8733 5 месяцев назад

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

  • @MikeCode-iy9sb
    @MikeCode-iy9sb 29 дней назад

    very good explaination. thanksSoMuch~

  • @nomanbangtan4120
    @nomanbangtan4120 8 месяцев назад +1

    Bro you should do a project video.

  • @pini5076
    @pini5076 5 месяцев назад

    thank you so much!

  • @pexhay4690
    @pexhay4690 8 месяцев назад

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

    • @ianfrye8988
      @ianfrye8988 8 месяцев назад

      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 8 месяцев назад

      @@ianfrye8988 thank you

  • @muhammetguloglu6268
    @muhammetguloglu6268 Месяц назад

    GOD BLESS YOU BRO!

  • @magomihaly1741
    @magomihaly1741 8 месяцев назад

    You re the man thx bro

  • @greengraphics5060
    @greengraphics5060 6 месяцев назад +1

    Do a node js video

  • @rukecodes
    @rukecodes 8 месяцев назад

    Thanks bro!!!!!!

  • @islamabdelhakiim7258
    @islamabdelhakiim7258 2 месяца назад

    thank you so much

  • @kathikr9360
    @kathikr9360 5 месяцев назад

    thank you dude

  • @manitamao6564
    @manitamao6564 4 месяца назад

    Thank u bro

  • @javohir1704
    @javohir1704 2 месяца назад

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

  • @user-mk2md9pu5m
    @user-mk2md9pu5m 8 месяцев назад +1

    django course please

  • @cyberblitz
    @cyberblitz 3 месяца назад

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

  • @kathikr9360
    @kathikr9360 3 месяца назад

    thanks again

  • @allhailalona
    @allhailalona Месяц назад

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

  • @devl0ver666
    @devl0ver666 2 месяца назад +1

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

  • @AdamGassouma
    @AdamGassouma Месяц назад +1

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

  • @constdev_
    @constdev_ Месяц назад

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

  • @ianfrye8988
    @ianfrye8988 8 месяцев назад

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