The snippets of JavaScript below contain syntax errors and/or logic errors. Iden
ID: 3674499 • Letter: T
Question
The snippets of JavaScript below contain syntax errors and/or logic errors. Identify the errors and insert your corrected code.
a. function main()
{
var total;
for(i = 0; i < 5; i++);
{
total =total + 1;
}
console.log("The total is " + total);
}
main();
b. function main()
{
var num;
var endProgram == 1;
while (endProgram = 0)
{
endProgram = prompt("Enter 1 to repeat the loop");
}
}
main();
c. function main()
{
var loopNum;
loopNum = Number(prompt("Enter the number of times to loop"));
var ctr = 1;
do
{
console.log("You have looped " + ctr + " times");
ctr = ctr + 1;
}
while (ctr <= loopNum)
}
main();
d. function main()
{
ctr = 1
while (ctr <= 10)
console.log ("The value of ctr is " + ctr);
ctr = ctr + 1
end while
}
main();
e. function main()
{
for (i=0; i < 10; i++)
{
console.log("i contains the value " + i);
i = i + 1;
}
}
main();
Explanation / Answer
a. function main()
{
var total;
for(i = 0; i < 5; i++);
{
total =total + i;//change here
}
console.log("The total is " + total);
}
main();
b. function main()
{
var num;
var endProgram = 1;change here
while (endProgram = 0)
{
endProgram = prompt("Enter 1 to repeat the loop");
}
}
main();
c. function main()
{
var loopNum;
loopNum = Number(prompt("Enter the number of times to loop"));
var ctr = 1;
do
{
console.log("You have looped " + ctr + " times");
ctr = ctr + 1;
}
while (ctr <= loopNum)
}
main();
d. function main()
{
ctr = 1
while (ctr <= 10){//change here
console.log ("The value of ctr is " + ctr);
ctr = ctr + 1
}
}
main();
e. function main()
{
for (i=0; i < 10; i++)
{
console.log("i contains the value " + i);
}//change here
}
main();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.