DEV Community

loading...

Discussion on: What’s your alternative solution? Challenge #5

Collapse
georgewl profile image
George WL • Edited

Could be be adapted to a oneliner, but I prefer readability over brevity.

const numbers = [...Array(10)].map((_,i)=>i+1);
const solution = [...numbers].reduce((a,b)=>a+b);

Adapted to a function which can take any number higher than 0:

function getSumOneToN(n){
     const numbers = [...Array(n)].map((_,i)=>i+1);
     return [...numbers].reduce((a,b)=>a+b);
}
Forem Open with the Forem app