Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

What will be the output of the following JavaScript code if oddsums(5); is executed after the following code snippet?
function oddsums(n)
{
let total = 0, result=[];
for(let x = 1; x <= n; x++)
{
let odd = 2*x-1;
total += odd;
result.push(total);
}
return result;
}

What will be the output of the following JavaScript code if oddsums(5); is executed after the following code snippet?
function oddsums(n)
{
let total = 0, result=[];
for(let x = 1; x <= n; x++)
{
let odd = 2*x-1;
total += odd;
result.push(total);
}
return result;
}
a) Returns [1,4,9,16,25]
b) Returns [1,2,3,4,5]
c) Returns [3,6,9,12,15]
d) Returns [1,3,5,7,9]

Answer: a
Explanation: Let keyword has block scope thus in the above code snippet the total variable will be defined inside the for loop. The above code returns 1, 4, 9, 16, 25 which is the square of the first five natural numbers.

Join The Discussion