...

View Full Version : JS beginner cant get code to work



hy_tek
12-10-2007, 02:43 AM
Hi..I am a 55 year old male who has decided I want to learn how to write JavaScript code. I have been doing online tutorials but I have run into a snag in a lesson. I have tried over and over to get this code to do what the author says it should but I am at a loss as to what I am doing wrong. i am typing it EXACTLY as the author but his code runs and mine does not. Please tell me what I am missing here.

The authors code is supposed to write a series of grades and the appropriate letter grade for each student.


<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>


The above code which was copy and pasted from the author runs properly.
Below is the code I typed myself in NoteTab Pro. To me it is identical to the code above but it won't run. Is this some rookie keyboard issue per chance? I have only num lock on.


<html>
<head>
<title>Grade Script</title>
<SCRIPT language="JavaScript">
<!--
function getGrade(name, grade){
var letterGrade
if(grade >= 90){
letterGrade = "A"
}
if(grade >= 80 && <90){
letterGrade = "B"
}
if(grade >= 70 && <80){
letterGrade = "B"
}
if(grade , 70){
letterGrade = "C"
}
document.write(name + " - " + letterGrade + "("+ grade + ")<BR>")
}
//-->
</SCRIPT>
</head>
<body bgcolor="white">
<p>
<script language="JavaScript" type="text/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></p>
</body>
</html>


The desired output results of this simple script is to print the following:

Mary - A (94)
Jim - C (70)
Albert - F (65)
Carroll - B (88)
Francis - F (44)
George - B (89)
Alice - A (90)
Sam - C (79)
Pat - B (80)
Ray - A (100)

I know this is a really basic problem to an advanced coder but I am just learning and have spent hours trying to figure the problem to no avail. Please can anyone shed some light on what I am doing wrong? :confused:

A1ien51
12-10-2007, 03:10 AM
You need to look at what you typed again. It is not exactly like the code above.

Eric

hy_tek
12-10-2007, 03:59 AM
I changed some of the characters in the "document.write" line that were erroneous but I still can't get it to run. The fact that some of the script tags are in caps and some not doesn't really make any difference does it? NotTab uses lower case while the author used upper case. This is so frustrating. I have written and re written to no avail.

A1ien51
12-10-2007, 04:04 AM
The problem is in your if statements, you are leaving out an important thing after the &&.

One thing that will make you life easier is to download Firefox [http://www.mozilla.com/en-US/firefox/] and install Firebug [http://www.getFirebug.com].

Firebug is a great tool to help you debug JavaScript errors.

Eric

hy_tek
12-13-2007, 01:38 PM
The problem is in your if statements, you are leaving out an important thing after the &&.

One thing that will make you life easier is to download Firefox [http://www.mozilla.com/en-US/firefox/] and install Firebug [http://www.getFirebug.com].

Firebug is a great tool to help you debug JavaScript errors.

Eric

Thank you so much I will try firebug....I need all the help I can get....:)

I looked at the script that works and to my eyes the code is identical after the "&&" in the "if" statements. Bare with me here I am new at this and I don't know what I left out.

aussiexile
12-14-2007, 06:16 AM
this is the copy and pasted statement from the author:

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

this is your code:

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

there is nothing to compare in the second part of the if statement, ie nothing to be less than 90

hope that helps

hy_tek
12-14-2007, 01:11 PM
this is the copy and pasted statement from the author:

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

this is your code:

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

there is nothing to compare in the second part of the if statement, ie nothing to be less than 90

hope that helps

Wow how obvious was that? I should have seen that mistake. I think perhaps I was trying to fix it for so long that I couldn't see the forest through the trees. I read and reread that code over and over and I can't believe I missed that. Thank you so much for pointing it out to me.....I can sleep now....lol



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum