Grouping Numbers algorithm with array reduce - Javascript Array

Javascript examples for Array:reduce

Description

Grouping Numbers algorithm with array reduce

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){// w  ww .  j ava 2  s  .  c o m

var arr = [4, 1, 2, 3, 4];
const out = arr.reduce((p, c) => {
  c % 2 === 0 ? p.unshift(c) : p.push(c)
  return p;
}, []);
console.log(JSON.stringify(out, null, 2));


    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials