Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-10-2007, 02:43 AM   PM User | #1
hy_tek
New Coder

 
Join Date: Mar 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
hy_tek is an unknown quantity at this point
JS beginner cant get code to work

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?

Last edited by hy_tek; 12-10-2007 at 03:49 AM..
hy_tek is offline   Reply With Quote
Old 12-10-2007, 03:10 AM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
You need to look at what you typed again. It is not exactly like the code above.

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 12-10-2007, 03:59 AM   PM User | #3
hy_tek
New Coder

 
Join Date: Mar 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
hy_tek is an unknown quantity at this point
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.
hy_tek is offline   Reply With Quote
Old 12-10-2007, 04:04 AM   PM User | #4
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
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
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 12-13-2007, 01:38 PM   PM User | #5
hy_tek
New Coder

 
Join Date: Mar 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
hy_tek is an unknown quantity at this point
Quote:
Originally Posted by A1ien51 View Post
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.

Last edited by hy_tek; 12-13-2007 at 03:08 PM..
hy_tek is offline   Reply With Quote
Old 12-14-2007, 06:16 AM   PM User | #6
aussiexile
New Coder

 
Join Date: Dec 2007
Posts: 16
Thanks: 2
Thanked 1 Time in 1 Post
aussiexile is an unknown quantity at this point
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
aussiexile is offline   Reply With Quote
Old 12-14-2007, 01:11 PM   PM User | #7
hy_tek
New Coder

 
Join Date: Mar 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
hy_tek is an unknown quantity at this point
Quote:
Originally Posted by aussiexile View Post
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
hy_tek is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:51 AM.


Advertisement
Log in to turn off these ads.