The below function will help us to find the sum of N Natural numbers using for..in loop
function SumofNNaturalNumbers() {
var N=10;
var sum = 0;
var numbers = [];
var count = 1;
while ( count <= N+1 ) numbers.push( count++ );
for(var number in numbers) { sum += parseInt(number);}
alert("Sum Of N Natural Numbers : " + sum);
}
We are looping through the numbers and taking a count variable and pushing the individual numbers to the numbers array. Then we have applied the for..in loop of javascript and summing the individual numbers. Finally, printing the same using the alert.