How to find the sum of all numbers between 1 and N using JavaScript -


i'm trying find way calculate sum of numbers between 1 n using javascript. following code have tried far doesn't seem work.

function numbersum(n) {   var total = 0;     for(var = 1; <= n; i++){       total += i;     }     return total; } 

i have tried using jslint , other validators online check if might have missed doesn't seem me find reason code not working either. there i'm missing above that's preventing script executing addition??

your code fine.

keep simple:

var res = (n * (n+1)) / 2; 

wiki.


Popular posts from this blog