OR
Q8(B). Write a program using JavaScript
Total - 05 Marks
Write a java script program to accept the marks of a student and then display the grades using a switch
case conditional statement.
Answers
Answer:
Here is an example of a JavaScript program that accepts the marks of a student and then displays the grades using a switch case conditional statement:
var marks = parseFloat(prompt("Enter the marks of the student: "));
switch (true) {
case (marks >= 90):
console.log("Grade: A");
break;
case (marks >= 80):
console.log("Grade: B");
break;
case (marks >= 70):
console.log("Grade: C");
break;
case (marks >= 60):
console.log("Grade: D");
break;
default:
console.log("Grade: F");
}
Explanation:
In this program, the prompt() function is used to accept input from the user and store it in a variable called marks. The parseFloat() function is used to convert the input from a string to a decimal number.
The switch statement is then used to determine the grade based on the marks. The switch statement takes an expression, in this case the value of marks, and compares it to the case expressions within the switch block. When a match is found, the code following that case statement is executed.
The case statements check if the marks are greater than or equal to a certain value. If the marks are greater than or equal to 90, the grade is "A" and the code breaks out of the switch statement. If the marks are greater than or equal to 80, the grade is "B" and the code breaks out of the switch statement. This continues for the other cases.
If none of the cases match, the code following the default case is executed, which assigns the grade "F" to the student.
Note: In this example, I have used 'console.log' statement to print the grade, but it can be done by creating a text element and replacing the text content with the grade.
More question and answers:
https://brainly.in/question/49603172
https://brainly.in/question/49435491
#SPJ1