Vous êtes sur la page 1sur 5

Name: Lab 3: If statements & Functions

Date:_______________

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>
Solution 3:
<HTML>

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

letterGrade = "B"
</HEAD>
}
<BODY bgcolor="white">

if(grade >= 70 && grade < 80){


<P>
<SCRIPT language="JavaScript">
<!--letterGrade = "C"
document.write("Mary - " + getStatus(94) + "<BR>")
document.write("Jim
} - " + getStatus(70) + "<BR>")
document.write("Albert - " + getStatus(65) + "<BR>")
document.write("Carroll
if (grade < 70){ - " + getStatus(88) + "<BR>")
document.write("Francis - " + getStatus(44) + "<BR>")
document.write("George
letterGrade = "F" - " + 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)
document.write(letterGrade + "<BR>" ) + "<BR>")
//-->
</SCRIPT>
}

</BODY>
//-->
</HTML>
</BODY>
</SCRIPT>
</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>

Vous aimerez peut-être aussi