28.5k views
4 votes
(50 POINTS) You are writing a program in JavaScript that asks users to input their grade level. You want to check for errors and give the users an error message if they 1) enter a blank, 2) enter letters instead of numbers, or 3) enter a number greater than 12. What would the pseudocode look like for a function that would check these things?

User Newy
by
7.7k points

2 Answers

2 votes

Answer:

Here is a pseudocode for a function in JavaScript that would check for errors in the user input of grade level:

function checkGradeLevelInput(gradeLevelInput) {

if (gradeLevelInput === '') {

return 'Error: Grade level cannot be blank.';

} else if (isNaN(gradeLevelInput)) {

return 'Error: Grade level must be a number.';

} else if (gradeLevelInput > 12) {

return 'Error: Grade level cannot be greater than 12.';

} else {

return 'Grade level is valid.';

}

}

Step-by-step explanation:

The function takes one parameter, gradeLevelInput, which is the user's input of their grade level. The function checks for three types of errors:

If the input is blank, the function returns an error message indicating that the grade level cannot be blank.

If the input is not a number, the function returns an error message indicating that the grade level must be a number. This is checked using the isNaN() function which returns true if the input is not a number.

If the input is greater than 12, the function returns an error message indicating that the grade level cannot be greater than 12.

If none of the error conditions are met, the function returns a message indicating that the grade level is valid. This function can be called whenever the user inputs their grade level to ensure that it is a valid input.

Step-by-step explanation:

User Ken Zira
by
7.3k points
3 votes

Answer:

function checkGradeLevel(gradeInput) {

if (gradeInput === "") { // checks if input is blank

return "Error: Please enter your grade level.";

} else if (isNaN(gradeInput)) { // checks if input is not a number

return "Error: Please enter a number for your grade level.";

} else if (gradeInput > 12) { // checks if input is greater than 12

return "Error: Please enter a grade level between 1 and 12.";

} else {

return "Grade level is valid."; // returns success message if input is valid

}

}

In this example, the function is called checkGradeLevel, and it takes one argument, gradeInput, which is the user's input for their grade level. The function checks for errors using a series of if statements:

The first if statement checks if the input is blank, using the === operator to compare the input to an empty string (""). If the input is blank, the function returns an error message telling the user to enter their grade level.

The second if statement checks if the input is not a number, using the isNaN() function. If the input is not a number, the function returns an error message telling the user to enter a number for their grade level.

The third if statement checks if the input is greater than 12, using the > operator. If the input is greater than 12, the function returns an error message telling the user to enter a grade level between 1 and 12.

If none of the if statements are true, the function returns a success message telling the user that their grade level is valid. You can customize the error messages to fit your specific needs.

User Rob Reuss
by
7.6k points