Author Topic: In Need of some JavaScript help  (Read 1222 times)

Offline Doomkiller

  • Ultra Heavyweight
  • Posts: 2112
  • Rep: 1
  • I HAS DOUBLE PISTOLS
    • View Profile
    • Awards
In Need of some JavaScript help
« on: June 22, 2011, 06:22:50 AM »
Alright, to anyone that knows JavaScript, please tell me why the following code will not work, and how to make it work
Code: [Select]
<HTML>
<HEAD>
<TITLE>Maths Quiz</TITLE>
<SCRIPT LANGUAGE = JavaScript>
var problemCount;
var doTen=false;
var mistakes=0;
var operation;


function Operator(message)
{
    var operation;
    if (message == " ") operation=" ";
    if (message == "-") operation="-";
    if (message == "*") operation="*";
    if (message == "/") operation="/";
    return (operation);
}

function newProblem()
{   
    var operation
    operation = Operator();
    document.quiz.firstone.value=4 rollDice(6);
    document.quiz.operator.value = operation;
      document.quiz.second.value=rollDice(4);
      document.quiz.attempt.value="";
}

function checkAnswer()
{

    var operation =document.quiz.operator.value;
    var a = parseInt(document.quiz.firstone.value);
/* ensure Javascript knows they are integers*/
    var b = parseInt(document.quiz.second.value);
    var answerA = a b;
    var answerS = a-b;
    var answerM = a*b;
    var answerD = a/b
    var attempt = document.quiz.attempt.value;
    if (operation == " ") ok=check(attempt,answerA);
    if (operation == "-") ok=check(attempt,answerS);
    if (operation == "*") ok=check(attempt,answerM);
    if (operation == "/") ok=check(attempt,answerD);
    if (doTen && ok)
    {   
        newProblem();
        problemCount  ;
    }
}

function check(attempt,answer)
{
    var ok;
    if (attempt==answer)
    {
        document.quiz.attempt.value = "YES";
        ok=true;
        document.face.src="happy.gif";
    }
    else
    {
        document.quiz.attempt.value = "TRY AGAIN";
        ok=false;
        mistakes  ;
    }
    return(ok);
}

function rollDice(sides)
{         
   var randNumber;
   var rollValue;
   randNumber=Math.random();
   randNumber=randNumber*sides;
   randNumber= randNumber  0.5;
   rollValue=Math.round(randNumber);
   return(rollValue);
}


</SCRIPT>
</HEAD>
<BODY>
<CENTER><H2> Maths Quiz </H2></CENTER>
<BR>
<FORM NAME = "quiz">
<INPUT TYPE="button" VALUE="Addition" onClick="Operator(" ")";>
<INPUT TYPE="button" VALUE="Subtraction" onClick="Operator("-")";>
<INPUT TYPE="button" VALUE="Multiplication" onClick="Operator("*")";>
<INPUT TYPE="button" VALUE="Division" onClick="Operator("/")";><BR>
<INPUT TYPE="button" VALUE="Get new problem" onClick= "newProblem()";><p>
<INPUT NAME="firstone" TYPE="text"  SIZE=5 >
<INPUT NAME="operator" TYPE="text" SIZE=5 >
<INPUT NAME="second" TYPE="text" SIZE=5 >
<INPUT TYPE = "text" NAME = "attempt" SIZE=9><p>   
<INPUT NAME="try" TYPE="button" VALUE="Check my answer" onClick= "checkAnswer()">
</FORM>
<CENTER><IMG NAME = face SRC="happy.gif">
</BODY> </HTML>


Right, the intention was to get it for the user to choose what the operator is for the question, but it keeps poping up with undefined. Any suggestions or help would be well, helpful. Thanks.
"I make death fun!"
Quote from: NerdCubed
OH MY GOD IT'S JAWS!

Offline Serge

  • *
  • Posts: 1530
  • Rep: 13
    • View Profile
    • http://www.q3k.org/
    • Awards
Re: In Need of some JavaScript help
« Reply #1 on: June 22, 2011, 12:27:29 PM »
If you use firefox, install Firebug. If you use Chrome, use the pre-installed Developer Tools. BOth help a lot with Java Script debugging.
home | twitter | yt | gmf de/compiler | component freedom | xmpp: q3k@q3k.org | email: q3k@q3k.org

Offline Doomkiller

  • Ultra Heavyweight
  • Posts: 2112
  • Rep: 1
  • I HAS DOUBLE PISTOLS
    • View Profile
    • Awards
Re: In Need of some JavaScript help
« Reply #2 on: June 23, 2011, 02:34:01 AM »
Alright, I figured out how to make it work early this morning.
Should've realised that returning a value to the button that was pressed kinda doesn't do anything
Oh and Serge, thanks for the Firebug suggestion, will go get it now.

If anyone wants to have a look at the code (as it is now, not currently finished) here it is:

Code: [Select]
<HTML>
<HEAD>
<TITLE>Maths Quiz</TITLE>
<SCRIPT LANGUAGE = JavaScript>
var problemCount;
var doTen=false;
var mistakes=0;
var operation;


function getOperator(a)
{
var operation;
operation=a;
document.quiz.operator.value=operation;
}

function newProblem()
{
var operation
operation = document.quiz.operator.value
document.quiz.firstone.value=4 rollDice();
document.quiz.operator.value = operation;
  document.quiz.second.value=rollDice();
  document.quiz.attempt.value="";
}

function checkAnswer()
{

var operation =document.quiz.operator.value;
var a = parseInt(document.quiz.firstone.value);
/* ensure Javascript knows they are integers*/
var b = parseInt(document.quiz.second.value);
var answerA = a b;
var answerS = a-b;
var answerM = a*b;
var answerD1 = a/b
var answerD2 = Math.round(answerD1*Math.pow(10,2))/Math.pow(10,2);
var attempt = document.quiz.attempt.value;
if (operation == " ") ok=check(attempt,answerA);
if (operation == "-") ok=check(attempt,answerS);
if (operation == "*") ok=check(attempt,answerM);
if (operation == "/") ok=check(attempt,answerD2);
if (doTen && ok)
{
newProblem();
problemCount  ;
}
}

function check(attempt,answer)
{
var ok;
if (attempt==answer)
{
document.quiz.attempt.value = "YES";
ok=true;
}
else
{
document.quiz.attempt.value = "TRY AGAIN";
ok=false;
mistakes  ;
}
return(ok);
}

function rollDice()

    var randNumber;
    var rollValue;
    var num1;
    var num1=document.quiz.message.value
if (num1=="You have chosen easy")
num1=10;
if (num1=="You have chosen normal")
num1=50;
if (num1=="You have chosen hard")
num1=350;
    randNumber=Math.random();
    randNumber=randNumber*num1;
    randNumber= randNumber  0.5;
    rollValue=Math.round(randNumber);
    return(rollValue);
}

function difficult(message)
{
if (message==1)
document.quiz.message.value="You have chosen easy";
if (message==2)
document.quiz.message.value="You have chosen normal";
if (message==3)
document.quiz.message.value="You have chosen hard";
}
</SCRIPT>
</HEAD>
<BODY>
<CENTER><H2> Maths Quiz </H2></CENTER>
<BR>
<FORM NAME = "quiz">
<INPUT TYPE="button" VALUE="Easy" onClick="difficult('1')">
<INPUT TYPE="button" VALUE="Normal" onClick="difficult('2')">
<INPUT TYPE="button" VALUe="Hard" onClick="difficult('3')"><BR>
<INPUT NAME="message" TYPE="text" SIZE=40><BR>
<INPUT TYPE="button" VALUE="Addition" onClick="getOperator(' ')">
<INPUT TYPE="button" VALUE="Subtraction" onClick="getOperator('-')">
<INPUT TYPE="button" VALUE="Multiplication" onClick="getOperator('*')">
<INPUT TYPE="button" VALUE="Division" onClick="getOperator('/')"><BR>
<INPUT TYPE="button" VALUE="Get new problem" onClick= "newProblem()";><p>
<INPUT NAME="firstone" TYPE="text"  SIZE=5 >
<INPUT NAME="operator" TYPE="text" SIZE=5 >
<INPUT NAME="second" TYPE="text" SIZE=5 >
<INPUT TYPE = "text" NAME = "attempt" SIZE=9><p>
<INPUT NAME="try" TYPE="button" VALUE="Check my answer" onClick= "checkAnswer()">
</FORM>
<CENTER>
</BODY> </HTML>

Just shove it into a notepad document, then save as a html file.
"I make death fun!"
Quote from: NerdCubed
OH MY GOD IT'S JAWS!

Offline nightcracker

  • *
  • Posts: 505
  • Rep: 7
  • Script kiddo
    • View Profile
    • NC Labs
    • Awards
  • Skype: orsonpeters
Re: In Need of some JavaScript help
« Reply #3 on: June 23, 2011, 06:40:35 AM »
Your HTML is HORRIBLE! Don't use caps for HTML tags (with the exception of DOCTYPE), so <html> instead of <HTML>. Please, always specify a doctype, I prefer HTML5 (the doctype is simply <!DOCTYPE HTML>). The correct tag for in-line javascript is <script type="text/javascript"> /* */ </script> and you forgot to close a <center> (all of that I saw in 2 seconds, didn't look that intensive).

If you want to look at good HTML style, check out the source of my website, http://nclabs.org/.

Offline Doomkiller

  • Ultra Heavyweight
  • Posts: 2112
  • Rep: 1
  • I HAS DOUBLE PISTOLS
    • View Profile
    • Awards
Re: In Need of some JavaScript help
« Reply #4 on: June 26, 2011, 08:57:03 PM »
Hmm, didn't know about the doctype, we havnt been told about that in SDD :/
The rest of the stuff was because I pulled the basic code out of a tutorial the teacher gave us, to which he said was a mixed batch due to being very old. Ive fixed it up, and thanks for saying what's the correct tag for javascript, it always changed in the tutorials and I didn't know which one was which...

Anyway, finished product, hopefully :D
:
https://gametechmods.com/uploads/files/Maths Quiz.rar

Not really what I wanted, I wouldve liked if it kept the styling to the other page when you click check answer, but I have no idea how to do that :/
"I make death fun!"
Quote from: NerdCubed
OH MY GOD IT'S JAWS!