The snippets of JavaScript below contain syntax errors and/or logic errors. Iden
ID: 3682984 • 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 birthYear = Number(prompt("Enter your birth year"));
while (birthYear < 0 or birthYear > 2020)
{
console.log("The year entered was not greater than 0");
console.log(" or less than 2020. ");
birthYear = Number(prompt("Enter your birth year"));
}
}
main();
b.function main()
{
var score;
do
{
score = Number(prompt("Enter the score"));
}
while (score < 0 || score > 100)
}
main();
c.function main()
{
var endProgram = prompt("Do you want to end the program, enter YES or NO");
while (endProgram != "NO" || endProgram != "YES")
{
console.log("Invalid input");
endProgram = prompt("Do you want to end the program, enter YES or NO");
}
}
main();
d.function main()
{
var letterGrade = prompt("Enter a grade of S or U for the project");
while (letterGrade != "S" and letterGrade != "U")
{
console.log("Valid letter grades are S or U");
letterGrade = prompt("Enter a grade of S or U for the project");
}
}
main();
e.function main()
{
var multiplier = prompt("Enter a value between 10 and 20");
if (multiplier < 10 || multiplier > 20)
{
console.log("The value was not between 10 and 20");
multiplier = prompt("Enter a value between 10 and 20");
}
}
main();
Explanation / Answer
In Java, all function definitions must be inside classes. We also call functions methods. Let's look at an example method
public class Main {
public static void foo {
var birthYear = Number(prompt("Enter your birth year"));
while (birthYear < 0 or birthYear > 2020)
{
console.log("The year entered was not greater than 0");
console.log(" or less than 2020. ");
birthYear = Number(prompt("Enter your birth year"));
}
}
}
Class score ()
{
var score;
do
{
score = Number(prompt("Enter the score"));
}
while (score < 0 || score > 100)
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.