JavaScript Debugging Exercises [1 point each, 5 points total] The snippets of Ja
ID: 3667627 • Letter: J
Question
JavaScript Debugging Exercises [1 point each, 5 points total]
The snippets of JavaScript below contain syntax errors and/or logic errors. Identify the error(s) and insert your corrected code.
a.
function main()
{
var num1 = Number(prompt("Enter your first number "));
var num2 = Number(prompt("Enter your second number "));
var result = computeTotal(num1, num2);
console.log("The total of your numbers is " + result);
}
function computeTotal(num1, num2)
{
return (num1 + num2);
}
b.
function main()
var stuName;
var stuAge;
stuName = getStudentName(stuName);
stuAge = getStudentAge(stuAge);
console.log("The student name is " + stuName + " and age is " + stuAge);
end function
function getStudentName(stuName)
stuName = prompt("Enter the name of the student");
return stuName;
end function
function getStudentAge(stuAge)
stuAge = prompt("Enter the age of the student");
return stuAge;
end function
main();
c.
function main()
{
var income = Number(prompt("Enter your monthly income "));
var taxes = computeTaxes();
console.log("The taxes owed on your income is " + taxes);
}
function computeTaxes(income, taxes)
{
taxes = income * .20;
return taxes;
}
main();
d.
function main()
{
var income = Number(prompt("Enter your monthly income "));
call displayMonthyIncome(income);
}
function displayMonthlyIncome(income)
{
console.log("Your income is " + income);
}
main();
e.
function main()
{
var total;
var val1 = Number(prompt("Enter a number "));
total = computeTotal(total, val1);
}
function computeTotal(total, val1);
{
total = total + val1;
return total
}
main();
Explanation / Answer
a)
ans:
function main()
{
var num1 = Number(prompt("Enter your first number "));
var num2 = Number(prompt("Enter your second number "));
var result = computeTotal(num1, num2);
console.log("The total of your numbers is " + result);//it will display in inner html for debug or want show the result press f12
//or using document.write() you can see the result in html page
}
function computeTotal(num1, num2)
{
return (num1 + num2);
}
B)
ans: correct answeer and in place of console.log use document.write
function main()
{
var stuName;
var stuAge;
stuName = getStudentName();
stuAge = getStudentAge();
document.write(stuName+""+stuAge);
console.log("The student name is " + stuName + " and age is " + stuAge);
}
//end function
function getStudentName()
{
var stuName = prompt("Enter the name of the student");
return stuName;}
//end function
function getStudentAge()
{
var stuAge = prompt("Enter the age of the student");
return stuAge;}
//end function
C)
ans: correct solution
function main()
{
var income = Number(prompt("Enter your monthly income "));
var taxes = computeTaxes(income);
console.log("The taxes owed on your income is " + taxes);
}
function computeTaxes(income)
{
var taxes = income * .20;
return taxes;
}
D)
ans:correct answeer is
function main()
{
var income = Number(prompt("Enter your monthly income "));
displayMonthlyIncome(income);
}
function displayMonthlyIncome(income)
{
console.log("Your income is " + income);
}
E)
ans:there is nothing to display and only one number for addition we need two numbers or adding number to initial value you should define first
function main()
{
var total=0;
var val1 = Number(prompt("Enter a number "));
total = computeTotal(total, val1);
//for display use document.write for html and inner html use console.log
}
function computeTotal(total, val1);
{
var total1 = total + val1;
return total1
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.