The snippets of JavaScript below contain syntax errors and/or logic errors. Iden
ID: 3679981 • 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 num1 = getNumber();
var num2 = getNumber();
}
function getNumber()
{
var input = Number(prompt("Enter a number"));
}
main();
B. function main()
{
var total = 0;
for (var ctr = 0; ctr < 10; ctr++)
{
total = computeTotal(total);
}
}
function computeTotal(total, ctr)
{
total = total + ctr;
return total;
}
main();
C. function main()
{
var playerName1;
var playerName2;
playerName1, playerName2 = getPlayerNames();
}
function getPlayerNames()
{
var name1 = prompt("Enter the name of player1");
return name1;
var name2 = prompt("Enter the name of player2");
return name2;
}
main();
D. module main()
{
var ctr = 1;
while (ctr <= 10)
{
console.log("The value of ctr is " + ctr);
ctr = ctr + 1;
}
}
main();
E. function main()
{
var stuName = prompt("Enter the name of your student");
var stuScore = Number(prompt("Enter the test score for the student"));
var output = displayOutput(stuScore, stuName);
console.log(output);
}
function displayOutput(stuName, stuScore)
{
var output = "The student name is " + stuName + ".";
var output = output + " The student score is " + stuScore + ".";
return output;
}
main();
Explanation / Answer
Corrected Code is in bold have a look
A) here we need to return input from function getNumber().
function main()
{
var num1 = getNumber();
var num2 = getNumber();
}
function getNumber()
{
var input = Number(prompt("Enter a number"));
return input;
}
main();
B) Here we need to pass ctr in computeTotal function call
function main()
{
var total = 0;
for (var ctr = 0; ctr < 10; ctr++)
{
total = computeTotal(total, ctr);
}
}
function computeTotal(total, ctr)
{
total = total + ctr;
return total;
}
main();
C) Here we have used more than on return statement in getPlayerNames() function which is not correct.
function main()
{
var playerName1;
var playerName2;
playerName1 = getPlayer1Name();
playerName2 = getPlayer2Name();
}
function getPlayer1Name()
{
var name1 = prompt("Enter the name of player1");
return name1;
}
function getPlayer2Name()
{
var name2 = prompt("Enter the name of player2");
return name2;
}
main();
D) Here in place of module use function
function main()
{
var ctr = 1;
while (ctr <= 10)
{
console.log("The value of ctr is " + ctr);
ctr = ctr + 1;
}
}
main();
E) Pass correct value in displayOutput() function
function main()
{
var stuName = prompt("Enter the name of your student");
var stuScore = Number(prompt("Enter the test score for the student"));
var output = displayOutput(stuName, stuScore);
console.log(output);
}
function displayOutput(stuName, stuScore)
{
var output = "The student name is " + stuName + ".";
var output = output + " The student score is " + stuScore + ".";
return output;
}
main();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.