...

View Full Version : Can I Divide 2 numbers then a 3rd number by the result



astronomyisfun
12-30-2010, 11:23 PM
I want to build a web form that will allow users to enter 3 different numbers and get a result. The answer is arrived at by the following. Enter 3 variable A, B, and C. Then A / B = D. Then C / D = the Answer.

Is there a way to set that up as a single click script? Everything I have tried has failed hard.:mad:

Dormilich
12-30-2010, 11:25 PM
what had you tried?

oesxyl
12-30-2010, 11:32 PM
I want to build a web form that will allow users to enter 3 different numbers and get a result. The answer is arrived at by the following. Enter 3 variable A, B, and C. Then A / B = D. Then C / D = the Answer.

Is there a way to set that up as a single click script? Everything I have tried has failed hard.:mad:
you need a idea how to get the input with a single click or something else?

"Please enter three distinct numbers separated with comma: ex 2,3,5"

best regards and happy holidays

Old Pedant
12-31-2010, 12:25 AM
Ummm...oesxyl, read what he said:


I want to build a web form ...


So no, I don't think that's the right answer.

oesxyl
12-31-2010, 12:45 AM
I want to build a web form that will allow users to enter 3 different numbers and get a result. The answer is arrived at by the following. Enter 3 variable A, B, and C. Then A / B = D. Then C / D = the Answer.

Is there a way to set that up as a single click script? Everything I have tried has failed hard.:mad:


Ummm...oesxyl, read what he said:
sometime i misunderstand what somebody say or do but i never try to answer without reading the question. :)


So no, I don't think that's the right answer.
which part in my anser stop him to build a webform?

best regards

Old Pedant
12-31-2010, 12:50 AM
Okay, I guess that would work. It would just be a clumsy web form.



<html>
<head>
<script type="text/javascript">
function calculate(form)
{
var A = parseFloat(form.A.value);
var B = parseFloat(form.B.value);
var C = parseFloat(form.C.value);
if ( isNaN(A) || isNaN(B) || isNaN(C) )
{
alert("All 3 values must be valid numbers!");
return;
}
var D = A/B;
form.answer.value = C/D;
}
</script>
</head>
<body>
<form>
Enter the three numbers:<ul>
<li>A: <input name="A"></li>
<li>B: <input name="B"></li>
<li>C: <input name="C"></li>
</ul>
<input type="button" value="Calculate!" onclick="calculate(this.form)">
<hr>
Answer: <input name="answer" readonly>
</form>
</body>


versus your version:


<html>
<head>
<script type="text/javascript">
function calculate(form)
{
var values = form.three.value.split(",");
if ( values.length != 3 ) {
alert("You did not enter 3 separate values with commas between");
return;
}
var A = parseFloat(values[0].replace(/\s/g,""));
var B = parseFloat(values[1].replace(/\s/g,""));
var C = parseFloat(values[2].replace(/\s/g,""));
if ( isNaN(A) || isNaN(B) || isNaN(C) )
{
alert("All 3 values must be valid numbers!");
return;
}
var D = A/B;
form.answer.value = C/D;
}
</script>
</head>
<body>
<form>
Enter three numbers separated by commas:
<input name="three">
<br>
<input type="button" value="Calculate!" onclick="calculate(this.form)">
<hr>
Answer: <input name="answer" readonly>
</form>
</body>

I know which form would make more sense to me, at least.

EDITED: To add in the JavaScript code, so that oesxyl will be happy. <grin/>

oesxyl
12-31-2010, 12:53 AM
Okay, I guess that would work. It would just be a clumsy web form.



<form>
Enter the three numbers:<ul>
<li>A: <input name="A"></li>
<li>B: <input name="B"></li>
<li>C: <input name="C"></li>
</ul>
<input type="button" value="Calculate!" onclick="calculate(this.form)">
</form>


versus your version:


<form>
Enter three numbers separated by commas:
<input name="three">
<br>
<input type="button" value="Calculate!" onclick="calculate(this.form)">
</form>

I know which form would make more sense to me, at least.
we are in javascript forum, i think op would be interested in javascript, :)

best regards

Old Pedant
12-31-2010, 01:05 AM
we are in javascript forum, i think op would be interested in javascript

Okay, okay. So now he has JavaScript.

Better?

Krupski
12-31-2010, 01:10 AM
Okay, I guess that would work. It would just be a clumsy web form.

EDITED: To add in the JavaScript code, so that oesxyl will be happy. <grin/>


Ha! I did some code too!



<html>
<head>
<title>Demo</title>
<script type="text/javascript">
function calculate(form)
{
var a = form.A.value; /* get input values into vars */
var b = form.B.value;
var c = form.C.value;

if (findBad(b)) { return; } /* check for divide by zero! */
var d = a / b; /* calc a/b */

if (findBad(d)) { return; } /* check for divide by zero! */
var answer = c / d; /* calc c/d */

var e = document.getElementById('result'); /* point to answer element */
e.innerHTML = 'The answer is: ' + answer; /* "print" text into answer span */
}
function findBad(test)
{
if (test == 0)
{
var e = document.getElementById('result');
e.innerHTML = 'Divide by zero error!'
return true;
}
return false;
}
</script>
</head>
<body>
<form>
Enter the three numbers:<ul>
<li>A: <input name="A"></li>
<li>B: <input name="B"></li>
<li>C: <input name="C"></li>
</ul>
<input type="button" value="Calculate!" onclick="calculate(this.form)">
</form>
<span id="result"></span>
</body>
</html>


Funny... you checked for NaN and I checked for divide-by-zero!


LOL! :D

oesxyl
12-31-2010, 01:12 AM
Okay, okay. So now he has JavaScript.

Better?
i don't know, op didn't answer to none of the questions until now, Dormilich and mine, :)

best regards and happy holidays, :)

Krupski
12-31-2010, 01:14 AM
Quick question.......


"hidden input name.value=text"

...vs...

"documentGetElementById('span_id').innerHTML=text"

....is there any reason to use one over the other?

Nile
12-31-2010, 01:18 AM
What are you talking about..? First of all, I have no idea what "hidden input name.value=text" is. Second of all:


documentGetElementById('span_id').innerHTML=text
Isn't valid

Old Pedant
12-31-2010, 01:57 AM
Quick question.......


"hidden input name.value=text"

...vs...

"documentGetElementById('span_id').innerHTML=text"

....is there any reason to use one over the other?

Personal preference? Laziness?

Nile
12-31-2010, 04:09 AM
Personal preference? Laziness?
Both of his codes are invalid though...?

Old Pedant
12-31-2010, 10:49 PM
He was just asking a general question and he was sloppy in the code he asked with.

So change the code to

hidden_input_name.value=text;

...vs...

document.getElementById('span_id').innerHTML=text

Though I admit I dunno why it would be a *hidden* input element. I think he misread my code, is all.

But in any case, his actual code was correct:

var e = document.getElementById('result');
e.innerHTML = 'The answer is: ' + answer;

Krupski
01-01-2011, 01:52 AM
He was just asking a general question and he was sloppy in the code he asked with.


Precisely correct. Thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum