View Full Version : Cant do Easy javascript program

Nov 30th, 2009, 04:29 AM
I am trying to create a javascript program that will assign a number based on a score. If the grade (number) is 90 or above it assigns a 4, if it is in the 80's its assigned a 3, etc.

So far:

<<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns = "http://www.w3.org/1999/xhtml">
<title> ITS200 Project 6: Quality Points </title>
<script type = "text/javascript">
function QualityPoints()
var form = document.getElementById( "form" );
var input = parseInt( form.number.value);

form.points.value = (input >= 0 && input <= 100)
Number(input): "Error";
} // end function

function Number(grade)
if (grade >= 90)
return 4;
else if (grade >=80)
return 3;
else if (grade >=70)
return 2;
else if (grade >=60)
return 1;
return 0;
} // end Number function
// -->
<form id = "form" action = "">
<table border = "1">
<td> Enter Grade
<input name = "number" type = "text" />
<input type = "button" value = "Get Quality Points" onclick = "QualityPoints()" />
<td> Quality Points
<input type = "text" name = "points" />

Where do I go from here? I have no idea whats wrong.

Nov 30th, 2009, 04:39 AM
What do you expect these lines to do?

form.points.value = (input >= 0 && input <= 100)
Number(input): "Error";

Nov 30th, 2009, 04:43 AM
To display an error if the number of input is not between 0 and 100

Nov 30th, 2009, 04:52 AM
It's not going to do that, and I suspect it won't do anything but throw an error. Here are two hints to start with:

To display an error if the number of input is not between 0 and 100

Nov 30th, 2009, 04:58 AM
Im thinking:

if form.points.value = (input > 0 or input < 100)
window.alert = ("Error: Number must be between 0 and 100");

Nov 30th, 2009, 05:08 AM
You're on the right track. Now you need to modify your if statement to use correct syntax and structure and that should work as expected. Then you still need to get the quality points back to the page, and that should be easy enough.

Nov 30th, 2009, 05:36 AM
I know I need parentheses around the condition of the if statement, but I'm getting burnt out. I'm considering deleting this part.

Would you recommend the following replacement or a variation thereof:

form.points.value =
( input >= 0 && input <= 100 ) ?
Number( input ) : "Invalid input.";

Philip M
Nov 30th, 2009, 08:52 AM
var input = parseInt(form.number.value);
if ((isNaN(input)) || (input <0) || (input >100)) {
alert ("Invalid input - must be a positive number between 0 and 100");
form.number.value = "";
return false;

Number with a capital N is a reserved word in Javascript. It is a good idea to avoid using number as an element or variable name as well.

The <!-- and //--> comment (hiding) tags have not been necessary since IE3. If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

From when I was a baby, I said that I would be the heavyweight world champion. - Dave Haye, BBC website.