Q:

Write a JavaScript program to find the maximum integer n such that 1 + 2 + ... + n <= a given integer

0

Write a JavaScript program to find the maximum integer n such that 1 + 2 + ... + n <= a given integer. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function sumn(val) {
  var sn = 0;
  var i = 0;
  while (sn <= val) {
    sn += i++;
  }
  return i - 2;
}
console.log(sumn(11))
console.log(sumn(15))

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now