Click to See Complete Forum and Search --> : i have this script, and, i have a math problem to go with it!


sciguyryan
09-26-2003, 02:10 PM
hi all,

here is my script, it asks the users for 2 values and, then adds them together but, the problem is that it gives the vauue like this: 1+2 and it would give the readout of 12?
can someone modify this?
also can any one modify this so that the script can accetp an applied mathamatical symblol from another prompt?@



please help, dsperate am i !!!!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
function(number1, number2)
{
number1 = window.prompt("Please enter a number to be assigned to the value number1");
number2 = window.prompt("Please enter a number to be assigned to the value number2");
added = (number1 + number2);
document.write(+added);
}
//-->
</SCRIPT>
</HEAD>
<BODY onload="add(number1, number2)">
</BODY>
</HTML>

Jona
09-26-2003, 02:23 PM
<script type="text/javascript"><!--
function mathPrompt(){
numA = prompt("Please enter an integer or decimal to be added", "");
numB = prompt("Please enter an integer or decimal to be added", "");

total = (parseFloat(numA) + parseFloat(numB));

alert(total);
}
//--></script>
</head>
<body onload="mathPrompt();">
<p>JavaScript only Math prompt. Equation: <i>x</i> + <i>y</i> = <i>z</i>.</p>
</body></html>


[J]ona

sciguyryan
09-26-2003, 02:31 PM
HI JONA....

UMM YOU

Jona
09-26-2003, 02:32 PM
Umm me? :confused:

[J]ona

sciguyryan
09-26-2003, 02:32 PM
HI JONA....

umm you forgot to end your function mathprompt with a }
though.
thanks you, you did better than me!

sciguyryan
09-26-2003, 02:34 PM
oh yea and by the way have you got an addon that allows yhe user to specify times, divide, take away etc????

Jona
09-26-2003, 02:37 PM
<script type="text/javascript"><!--
function mathPrompt(){
numA = prompt("Please enter an integer or decimal", "");
numB = prompt("Please enter an integer or decimal", "");
eqType = prompt("Please enter a multiplication (*), division (/), subtraction (-), or addition (+) sign", "");

total = (parseFloat(numA) + parseFloat(numB));
total = eval("parseFloat(numA)"+eqType+"parseFloat(numB)");

alert(total);
}
//--></script>
</head>
<body onload="mathPrompt();">
<p>JavaScript only Math prompt.</p>
</body></html>


[J]ona

sciguyryan
09-26-2003, 02:42 PM
sorry to bug you again but, haw could i specify this so that the user can add functions such as pi and things like that????
if it can be done can it be bone by entering somthing like pi+pi = whatever you get once ther added to gether and, what about the other functions such as square root???????



ps sorry to bug you wth this!
could u provide instructions on how to add the functons plz?

Jona
09-26-2003, 02:46 PM
I created a small calculator script which you should be able to learn from. The calculator script. (http://geocities.com/god_loves_07/calculator.html)

[J]ona

sciguyryan
09-26-2003, 02:51 PM
??? i dont get it as i have only just learned the js basics.
could u give me instuctions st to how to use this?????

Jona
09-26-2003, 03:01 PM
The code is quite elementary, so figuring it out shouldn't be difficult... I'll help, though.


<script type="text/javascript"><!--
var total; // set as a global variable

function mathPrompt(){
numA = prompt("Please enter an integer or decimal", ""); //prompt for number 1
numB = prompt("Please enter an integer or decimal", ""); //primpt for number 2
eqType = prompt("Please enter a multiplication (*), division (/), subtraction (-), or addition (+) sign", ""); //which kind of math sign to use?

total = eval("parseFloat(numA)"+eqType+"parseFloat(numB)"); //number 1 [sign used, could be *, /, - or +] number 2

alert(total);

if(confirm("Would you like the square root of this number?")){total=Math.sqrt(total); alert(total);} // alert the square root of the total

if(confirm("Would you like to multiply the total by Pi?")){
total=Math.PI*total; alert(total);}

}
//--></script>
</head>
<body onclick="mathPrompt();">
<p>JavaScript only Math prompt.</p>
</body></html>


[J]ona

sciguyryan
09-26-2003, 03:07 PM
ok, thanks i get it now, it was simple once you showd me how!

thanks i ow you one!

Jona
09-26-2003, 03:16 PM
You're welcome. :)

[J]ona

sciguyryan
09-27-2003, 03:09 AM
the script does not run?! can you help with this?!

Jona
09-27-2003, 12:15 PM
Replace onclick with onload in the BODY tag...

[J]ona

sciguyryan
09-27-2003, 01:25 PM
ok, LOL i didnt see that!

Jona
09-27-2003, 01:27 PM
Hehe...

[J]ona

sciguyryan
09-28-2003, 09:55 AM
ok, got it working but, now a question on the modification.
i am having problems modifying the script to allow the user to specify weather they want number 1 to be added, timesed, etc br the math function and specifying what they want it to de done by e.g. number1 *,/,-,+ by pi how could i leave the usr specify that?

Jona
09-28-2003, 01:40 PM
Wouldn't it be easier to use a form on the page, instead of prompts and alerts?

[J]ona

sciguyryan
09-28-2003, 02:41 PM
yes, mutch thats what im going to do now.... do you mind if i use your script as a Reference?

Jona
09-28-2003, 03:04 PM
Shoot five, it's all yours. :)

[J]ona

boojum
09-28-2003, 05:09 PM
any body know of a way to find out if a number is even? is there somethng built into Math or Number etc? if not, what is the minimalist way of determining of a number is even?

Jona
09-28-2003, 06:57 PM
function isEven(num){
if(num%2 == 0){
alert("Number is even");
} else {
alert("Number is odd");
&nbsp;&nbsp;}
}


[J]ona