Sign in
Log inSign up

reduce helper method in es6

Default profile photo
Anonymous
·Mar 21, 2018

// use the rediuce helper to create an object that tallies the number of sitting and standing desks.The object returned should have the form '{sitting:3,standing:2'}'

// What I am thinking that if var a ={s:0,m:0},then I can write (a.s)++,and it is perfectly valid and same thing I am trying to implement in the below code but it is giving error

var desks = [
  { type: 'sitting' },
  { type: 'standing' },
  { type: 'sitting' },
  { type: 'sitting' },
  { type: 'standing' }
];

var deskTypes = desks.reduce(function(start,iter) {
    if(iter.type==='sitting'){
      return (start.sitting=start.sitting+1);
    }

    else{

return (start.standing=start.standing+1);    }

}, { sitting: 0, standing: 0 });
Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct