Q. Among the following given JavaScript snipped codes, which is more efficient

Code:
Code 1

for(var number=10;number>=1;number--)  
{  
document.writeln(number);  
}  

Code 2:

var number=10;  
while(number>=1)  
{  
document.writeln(number);  
       number++;  
}  
  • (A) Code 1
  • (B) Code 2
  • (C) Both Code 1 and Code 2
  • (D) Cannot Compare
πŸ’¬ Discuss
βœ… Correct Answer: (A) Code 1
Explanation: Code 1 will be more efficient. In fact, the second code may encounter a runtime error because the value of "number" is never going to be equal to or less than one.
Explanation by: Lalit Singh
Code 1 will be more efficient. In fact, the second code may encounter a runtime error because the value of "number" is never going to be equal to or less than one.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
288
Total Visits
πŸ“½οΈ
3 y ago
Published
πŸŽ–οΈ
Lalit Singh
Publisher
πŸ“ˆ
97%
Success Rate