javascript - Function to add up all the numbers from 1 to num -
i think there better version solution. solution working fine. didn't juggling of values using loop.
but particularly wondering, if there nice , 1 line solution getting values in array entered number function.
function simpleadding(num) { var fnum = []; (var = 1; <= num; i++) { fnum.push(i); } return fnum.reduce(function(prevvalue, currentvalue) { return prevvalue + currentvalue; }) }
i think there better version solution.
use formula
function simpleadding(num) { return num * (num+1) /2; }
if there nice , 1 line solution getting values in array entered number function
function simpleadding(num) { var fnum = new array(num).join().split(',').map(function(item, index){ return ++index;}); return fnum.reduce(function(prevvalue, currentvalue) { return prevvalue + currentvalue; }) }
Comments
Post a Comment