PDA

View Full Version : function script not working


puddles
06-27-2002, 03:12 PM
Can anyone tell me what is wrong with this script it's tearing my hair out. As you can probably guess by the code I'm a beginner.

All help appreciated.
Cheers
Paul

<html>
<head>
<script>
<!--
var numCorrect=0;

function takeTest() {
var response="";
var points=0;

var q1 = "What is the price of the Teak Elephants?";
var a1 = "$55.00";

var q2 = From what type of wood is the lion carved?";
var a2 = "EBONY";

var q3 = "What is the last name of both carvers on this page?";
var a3 = "MANE";

response = prompt(q1,"$0.00");
if (response) points=runningTotal ((response==a1) ? 1 : 0);
alert(points);

response = prompt(q2,"");
if (response) points=runningTotal ((response.toUpperCase()==a2) ? 1 : 0);
alert(points);
response = prompt(q3,"");
if (response) points=runningTotal((response.toUpperCase()==a3) ? 1 : 0);
alert("You answered a total of " + points + " correctly.");
numCorrect = 0;
points = 0;
}

function runningTotal(i) {
numCorrect += i;
return numCorrect;

}

//-->
</script>

</head>

<form>
<input type="button" class="button" value="Quiz" onClick="takeTest();">
</form>


</body>
</html>

puddles
06-27-2002, 03:26 PM
-

Lee Brenner
06-27-2002, 03:31 PM
Put an opening quotation mark before "From" on q2.

Also (as a matter of coding practices), you don't need the form tags. You should add a <body> tag. You don't need the class attribute on the input. And you could give your input a name attribute.

tamienne
06-27-2002, 03:49 PM
Also (as a matter of coding practices), you don't need the form tags.

Actually, you should have the form tags. NS requires it to display the form.

puddles
06-27-2002, 04:08 PM
I was wondering that myself

Lee Brenner
06-27-2002, 04:08 PM
I stand corrected.

puddles
06-27-2002, 04:14 PM
But can anybody spot why this also isn't working?
Can you also tell me what you all use as an editor when you are coding?

Many thanks
Paul

<html>
<head>

<script language = "Javascript">
<!--

function returnValue() {
var sampleVar;
sampleVar = document.myForm.input1.value * document.myForm.input2.value;
return sampleVar;
}
//-->
</script>
</head>

<body>

<form name="myForm">
Multiply <input type="text" name="input1">
By <input type="text" name="input2">
<input type ="button" value="Result"
onClick=
"alert(The result is ' + returnValue() + '.');">
</form>

</body>
</html>

Lee Brenner
06-27-2002, 04:20 PM
Why not try this:


<html>
<head>

<script language = "Javascript">
<!--

function returnValue() {
var sampleVar;
sampleVar = document.myForm.input1.value * document.myForm.input2.value;
alert("Result is: " + sampleVar);
}
//-->
</script>
</head>

<body>

<form name="myForm">
Multiply <input type="text" name="input1">
By <input type="text" name="input2">
<input type ="button" value="Result" onClick="returnValue();">
</form>

</body>
</html>


I use Edit+2, the best darn editor there is!