Q. Consider 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 would be the output if
oddsums(5);
is executed afted the above code snippet ?

  • (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]
πŸ’¬ Discuss
βœ… Correct Answer: (A) Returns [1,4,9,16,25]
Explanation: The above code returns 1,4,9,16,25 which is the square of the first five natural numbers. Notice the usage of let keyword in the above code snippet.
Explanation by: Mr. Dubey
The above code returns 1,4,9,16,25 which is the square of the first five natural numbers. Notice the usage of let keyword in the above code snippet.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
142
Total Visits
πŸ“½οΈ
2 y ago
Published
πŸŽ–οΈ
Mr. Dubey
Publisher
πŸ“ˆ
84%
Success Rate