Vous êtes sur la page 1sur 5

c   



   
2 

1. Here is a simple problem for you to solve using an if else statement. Suppose you must

get a grade of 70 or better to get a passing grade for this class. Write a script that will test

to see if you passed and will display the results in the browser as following:

- If passed display "Congratulations, you passed."

- If not display "Sorry, you did not pass."

2. Write a script that contains a function getStatus() that you can pass a grade as a

parameter and have it display a passed or failed condition. Demonstrate the functions by

printing out the pass/fail status for five students. One example of one student could be:

document.write("Mary - ") + getStatus(94) + document.write("<BR>")

3. Make a final modification to the above script that a letter grade is displayed instead of

pass or fail. This letter grade should be based on the following:

A is a grade of 90 or greater

B is a grade of 80 or greater, but less that 90

C is a grade of 70 or greater, but less than 80

F is any grade below 70


Solutions 1-2:

<HTML>
<HEAD>
<TITLE>Grade Script</TITLE>
<SCRIPT language="JavaScript">
<!--
function getStatus(grade){
if(grade >= 70){
document.write("Congratulations, you passed.")
}
else{
document.write("Sorry, your did not pass.")
}
}
//-->
</SCRIPT>

</HEAD>
<BODY bgcolor="white">

<P>
<SCRIPT language="JavaScript">
<!--
document.write("Mary - ") + getStatus(94) + document.write("<BR>")
document.write("Jim - ") + getStatus(70) + document.write("<BR>")
document.write("Albert - ") + getStatus(65) + document.write("<BR>")
document.write("Carroll - ") + getStatus(88) + document.write("<BR>")
document.write("Francis - ") + getStatus(44) + document.write("<BR>")
//-->
</SCRIPT>

</BODY>
</HTML>
Solution 3:

<HTML>
<HEAD>
<TITLE>Grade Script</TITLE>
<SCRIPT language="JavaScript">
<!--
function getGrade(grade){
var letterGrade
if(grade >= 90){
letterGrade = "A"
}
if(grade >= 80 && grade < 90){
letterGrade = "B"
}
if(grade >= 70 && grade < 80){
letterGrade = "C"
}
if (grade < 70){
letterGrade = "F"
}
document.write(letterGrade + "<BR>" )
}
//-->
</SCRIPT>

</HEAD>
<BODY bgcolor="white">

<P>
<SCRIPT language="JavaScript">
<!--
document.write("Mary - ") + getGrade(94)
document.write("Jim - ") + getGrade(70)
document.write("Albert - ") + getGrade(65)
document.write("Carroll - ") + getGrade(88)
document.write("Francis - ") + getGrade(44)
document.write("George - ") + getGrade(89)
document.write("Alice - ") + getGrade(90)
document.write("Sam - ") + getGrade(79)
document.write("Pat - ") + getGrade(80)
document.write("Ray - ") + getGrade(100)

//-->
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Grade Script</TITLE>
<SCRIPT language="JavaScript">
<!--
function getStatus(grade){
if(grade >= 90){
return "A"
}
if(grade >= 80){
return "B"
}
if(grade >= 70){
return "C"
}
return "F"
}
//-->
</SCRIPT>

</HEAD>
<BODY bgcolor="white">

<P>
<SCRIPT language="JavaScript">
<!--
document.write("Mary - " + getStatus(94) + "<BR>")
document.write("Jim - " + getStatus(70) + "<BR>")
document.write("Albert - " + getStatus(65) + "<BR>")
document.write("Carroll - " + getStatus(88) + "<BR>")
document.write("Francis - " + getStatus(44) + "<BR>")
document.write("George - " + getStatus(89) + "<BR>")
document.write("Alice - " + getStatus(90) + "<BR>")
document.write("Sam - " + getStatus(79) + "<BR>")
document.write("Pat - " + getStatus(80) + "<BR>")
document.write("Ray - " + getStatus(100) + "<BR>")
//-->
</SCRIPT>

</BODY>
</HTML>
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>Grade Script</TITLE>
<SCRIPT language="JavaScript">
<!--
function getGrade(name, grade){
var letterGrade
if(grade >= 90){
letterGrade = "A"
}
if(grade >= 80 && grade < 90){
letterGrade = "B"
}
if(grade >= 70 && grade < 80){
letterGrade = "C"
}
if (grade < 70){
letterGrade = "F"
}
document.write(name + " - " + letterGrade + " (" + grade + ")<BR>" )
}
//-->
</SCRIPT>

</HEAD>
<BODY bgcolor="white">

<P>
<SCRIPT language="JavaScript">
<!--
getGrade("Mary", 94)
getGrade("Jim", 70)
getGrade("Albert", 65)
getGrade("Carroll", 88)
getGrade("Francis", 44)
getGrade("George", 89)
getGrade("Alice", 90)
getGrade("Sam", 79)
getGrade("Pat", 80)
getGrade("Ray", 100)

//-->
</SCRIPT>

</BODY>
</HTML>