View Full Version : Math Problem
TeKtRoN
09-07-2006, 10:05 PM
I am Definately new to java script. I have read and experimented with wc3schools webpage on java script. I just can't get the nac of it.
If anyone could help me I need.....
3 text boxes one named Sidea second named Sideb and the third named Sidec
I need a button that says solve. And be allowed to enter data in the first two boxes and press the button and the answer will pop up in the third. The formula for the third box is Sqrt((Sidea^2)+(Sideb^2)) that is the hypotenuse of a triangle. If you could help me with that. I think I could figure the rest out entirely. I hope thanks very Much.
shachi
09-08-2006, 05:19 AM
Try this:
<html>
<head>
<script type="text/javascript">
function calculate(){
var hypo = document.pyform.hypotenuse.value;
var perp = document.pyform.perpendicular.value;
var base = document.pyform.base.value;
if(hypo == "?" || hypo == ""){
calchypo();
} else if(perp == "?" || perp == ""){
calcperp();
} else if(base == "?" || base == ""){
calcbase();
} else {
document.getElementById("result").innerHTML = "";
document.getElementById("pseudo").innerHTML = "";
}
}
function calchypo(){
var perp = document.pyform.perpendicular.value;
var base = document.pyform.base.value;
var resultspace = document.getElementById("result");
var hyp = Math.sqrt(perp*perp + base*base);
resultspace.innerHTML = "h = "+hyp;
document.getElementById("pseudo").innerHTML = "h<sup>2</sup> = p<sup>2</sup> + b<sup>2</sup><br>";
document.getElementById("pseudo").innerHTML += "h<sup>2</sup> = ("+perp+")<sup>2</sup> + ("+base+")<sup>2</sup><br>";
document.getElementById("pseudo").innerHTML += "h<sup>2</sup> = "+perp*perp+" + "+base*base+"<br>";
document.getElementById("pseudo").innerHTML += "h<sup>2</sup> = "+parseInt(perp*perp + base*base)+"<br>";
document.getElementById("pseudo").innerHTML += "h = "+Math.sqrt(perp*perp + base*base)+"<br>";
}
function calcperp(){
var hypo = document.pyform.hypotenuse.value;
var base = document.pyform.base.value;
var resultspace = document.getElementById("result");
var perp = Math.sqrt(hypo*hypo - base*base);
resultspace.innerHTML = "p = "+perp;
document.getElementById("pseudo").innerHTML = "p<sup>2</sup> = h<sup>2</sup> - b<sup>2</sup><br>";
document.getElementById("pseudo").innerHTML += "p<sup>2</sup> = ("+hypo+")<sup>2</sup> - ("+base+")<sup>2</sup><br>";
document.getElementById("pseudo").innerHTML += "p<sup>2</sup> = "+hypo*hypo+" - "+base*base+"<br>";
document.getElementById("pseudo").innerHTML += "p<sup>2</sup> = "+parseInt(hypo*hypo - base*base)+"<br>";
document.getElementById("pseudo").innerHTML += "p = "+Math.sqrt(hypo*hypo - base*base)+"<br>";
}
function calcbase(){
var hypo = document.pyform.hypotenuse.value;
var perp = document.pyform.perpendicular.value;
var resultspace = document.getElementById("result");
var base = Math.sqrt(hypo*hypo - perp*perp);
resultspace.innerHTML = "b = "+base;
document.getElementById("pseudo").innerHTML = "b<sup>2</sup> = h<sup>2</sup> - p<sup>2</sup><br>";
document.getElementById("pseudo").innerHTML += "b<sup>2</sup> = ("+hypo+")<sup>2</sup> - ("+perp+")<sup>2</sup><br>";
document.getElementById("pseudo").innerHTML += "b<sup>2</sup> = "+hypo*hypo+" - "+perp*perp+"<br>";
document.getElementById("pseudo").innerHTML += "b<sup>2</sup> = "+parseInt(hypo*hypo - perp*perp)+"<br>";
document.getElementById("pseudo").innerHTML += "b = "+Math.sqrt(hypo*hypo - perp*perp)+"<br>";
}
</script>
<style type="text/css">
input[type=text] {
border: 1px solid #FF9900;
padding: 3px;
color: #B17704;
}
input[type=text]:focus {
background-color: #EEE1C8;
}
body {
font-family: Arial,Verdana,sans-serif,monospace;
}
</style>
</head>
<body>
<form name="pyform">
Hypotenuse(h<sup>2</sup>) : <input type="text" name="hypotenuse">
Perpendicular(p<sup>2</sup>) : <input type="text" name="perpendicular">
Base(b<sup>2</sup>) : <input type="text" name="base">
<input type="button" onclick="calculate();" value="calculate">
</form>
<font color="red">Result:</font>
<br>
<span id="result"></span>
<br>
<font color="red">Synthesis:</font>
<div id="pseudo">
</div>
</body>
</html>
TeKtRoN
09-08-2006, 12:48 PM
Believe it or not that was too perfect, minus no output was in textbox.
But I really need it even simpler than that. And the boxes in order like below if you could make the script work with I have below then I will understand it perfectly. And to Prove myself to be worthy. I will write the second half of the formula and the third on my own. YOU merged all of them together which is way above and beyond my expectations. I just need it a little simpler and output in a textbox. not on screen below. Thanks That was really kick A#&
This acutally being the text box.
----------- v
Sidea = (textbox1) Sideb = (textbox2) Sidec = (textbox3)
_______________________________________________^Use this formula here : {sidec = Sqrt((sidea^2)+(sideb^2))}
The carrot is for the power of does that work in Java Script?
I really need it simple as possible for me to understand the rest.
mwinter
09-08-2006, 06:50 PM
Use this formula here : {sidec = Sqrt((sidea^2)+(sideb^2))}
To obtain the square root of a number, use the Math.sqrt method. To square a number, either multiply it by itself, or use the Math.pow method with 2 as the second argument.
Math.sqrt(sideA * sideA + sideB * sideB)
The carrot
Caret. :)
is for the power of does that work in Java Script?
No, it is the bit-wise XOR operator.
If you want guidance for the other actions you'll need to perform, you might want to start by reading the comp.lang.javascript FAQ (http://www.jibbering.com/faq/). Entries 4.6 and 4.13 will be the most useful (including the documents they reference).
Hope that helps (and feel free to ask for more help if you need it),
Mike
TeKtRoN
09-09-2006, 03:55 AM
I appreciate your help I believe I need to study this awhile I understand about the xor situtation with java script vs visual basic. I am working on the rest of it. I believe it might take me a few days if lucky to catch adrift of things. Thanks for your time you definately know what you are talking about. I kinda wish I could catch on about 100 times faster. But given the circumstances I will leave it at that. Again Thanks TeKtRoN
TeKtRoN
09-11-2006, 07:40 AM
Again Thanks you are the Greatest and my all time favorite saying is (Education is a terrible thing to waste. You definately didn't waste any.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.