I am struggling to find the syntax or logic errors in the following 5 (a, b, c,
ID: 3667626 • Letter: I
Question
I am struggling to find the syntax or logic errors in the following 5 (a, b, c, d, & e) problems. Please help me with each one!!! 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.
// call to main() function is missing
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);
}
main();
b.
// syntax for defining a function in java script is wrong
// Body of function should be enclosed in curly braces {}
// removed end function
function main()
{
var stuName;
var stuAge;
stuName = getStudentName(stuName);
stuAge = getStudentAge(stuAge);
console.log("The student name is " + stuName + " and age is " + stuAge);
}
function getStudentName(stuName)
{
stuName = prompt("Enter the name of the student");
return stuName;
}
function getStudentAge(stuAge)
{
stuAge = prompt("Enter the age of the student");
return stuAge;
}
main();
c.
// computeTaxes() function takes two arguments, income and taxes
// declare a variable taxes first and pass it to computeTaxes along with income
function main()
{
var income = Number(prompt("Enter your monthly income "));
var taxes;
taxes = computeTaxes(income, taxes);
console.log("The taxes owed on your income is " + taxes);
}
function computeTaxes(income, taxes)
{
taxes = income * .20;
return taxes;
}
main();
d.
// call is unknown in javscript
// we can directly call a function using its name
function main()
{
var income = Number(prompt("Enter your monthly income "));
displayMonthyIncome(income);
}
function displayMonthlyIncome(income)
{
console.log("Your income is " + income);
}
main();
e.
// total variable is used without initializing
// Any variable has to be initialized with a value before using it
function main()
{
var total = 0;
var val1 = Number(prompt("Enter a number "));
total = computeTotal(total, val1);
alert("Total is " + total);
}
function computeTotal(total, val1);
{
total = total + val1;
return total
}
main();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.