Javascript Function Creation Question 11

Introduction

Implement a function sigma(n) that, given a number n, returns the sum of all possible integers from 1 up to n (inclusive).

For example,

sigma(3) = 1+2+3; 
sigma(5) = 1+2+3+4+5. 
function sigma(num) {
  //your code
}
var sigma_result = sigma(5);
console.log(sigma_result);



function sigma(num) {
  if (num < 0) {
    return "The given argument must be positive integer.";
  }
  var sig = 0;
  for (var i = 1; i <= num; i++) {
    sig += i;
  }
  return sig;
}
var sigma_result = sigma(5);
console.log(sigma_result);



PreviousNext

Related