Chapter 6: Conditional Operators in JavaScript
กก
We have learned the If operator in
Chapter 5, but now we will use If and else together as conditional
operators so that the program executes certain jobs according to certain
conditions. The if and else constitute a program structure, where it will
select certain alternatives during program execution. I am going to illustrate
this concept using an example which involve the computation of students'
examination grades based on their marks. The program will compute the grade
based on the mark entered and output it in a table. The codes are shown below,
you can copy and paste them into your webpage and run the program.
Example:
|
กก
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Examination Grade</title>
<H2><font face="Arial">The grade will be shown below when you enter the
mark</font></H2>
<table border=1 align=center bgcolor=yellow ><tr><td><b><script
language=javascript>
var mark;
mark=window.prompt("Enter your mark:","0")
if (mark>=80)
document.writeln("Grade="+"A");
else
if (mark>=60)
document.writeln("Grade="+"B");
else if (mark>=50)
document.writeln("Grade="+"C");
else if (mark>=40)
document.writeln("Grade="+"D");
else if (mark>=30)
document.writeln("Grade="+"E");
else
document.writeln("F");
</script></td></tr></table>
</head>
<body>
</body>
</html>
กก
Click on the
testing page |
กก
<Previous
Chapter> [Back
to Main Page]<Next Chapter>