Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 2 of 2
Thread: JavaScript algorithm not working

01172013, 01:11 AM #1
 Join Date
 Oct 2011
 Location
 Geraldton, Ontario
 Posts
 155
 Thanks
 1
 Thanked 1 Time in 1 Post
JavaScript algorithm not working
Hello,
I've been working on a basic script that determines a net value based on four userinput values. After all the values have been stored, the operators do their work in calculating the final solution. If you notice below, the values in the comments are what the literal version of the equation is made up of; (x2x1) + (x3x2) + (x4x3)= y. In reference, if the snippetnj
and the other variables were converse, the answers came out to be opposite from each other, as in the absolute or negativity value.
PHP Code:<!DOCTYPE HTML PUBLIC "//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
function jah()
{
var PH = document.getElementById('myDiv');
if (PH)
PH.innerHTML = "?";
}//end of jah function
</script>
<script type="text/javascript">
function fourFunction()
{
var n=prompt("What is the first value?","0");
n=parseFloat(n);
if(isNaN(n))
{
alert("This input cannot be parsed into a number.");
return;//x1
}
else
var j=prompt("What is the second value?","0");
j=parseFloat(j);
if(isNaN(j))
{
alert("This input cannot be parsed into a number.");
return;//x2
}
else
var k=prompt("What is the third value?","0");
k=parseFloat(k);
if(isNaN(k))
{
alert("This input cannot be parsed into a number.");
return;//x3
}
else
var v=prompt("What is the fourth value?","0");
v=parseFloat(v);
if(isNaN(v))
{
alert("This input cannot be parsed into a number.");
return;//x4
}
else
var u = nj
var q = jk
var s = kv
var b = u+q+s
var MD = document.getElementById('myDiv');
if (MD)
MD.innerHTML = b;
}//end of fourFunction()
</script>
</head>
<center>
<h1>Net Value Calculator 1.0</h1>
<p>This script calculates the net value given a range of respective integers.</p>
<button OnClick="fourFunction()">Create List</button>
<br />
<button OnClick="jah()">Reset</button>
<br /><br />
<div id=myDiv>?</div>
</center>
</html>
Last edited by Taro; 01172013 at 01:14 AM.

01172013, 01:48 AM #2
Ummm...I think you need to go back and take prealgebra again.
If you do:
x1 = 0;
x2 = 4;
x3 = 0;
x4 = 0;
Then you do:
y = (x2x1) + (x3x2) + (x4x3);
you will be doing:
y = (4  0) + (0  4) + (0  0)
which is
y = (4) + (4) + 0
which is, correctly:
y = 0
*********
In any case, just going one step further into elementary algebra, why not SIMPLIFY this equation:
y = (x2x1) + (x3x2) + (x4x3);
which becomes:
y = x2x1+x3x2+x4x3;
which becomes
y = x2x2 + x3x3  x1 + x4
which becomes
y = 0 + 0  x1 + x4
which becomes
y = x4  x1
In other words, the values entered for x2 and x3 *DO NOT MATTER*. The end result will only depend on x1 and x4.
Me thinks you have much much bigger problems than just your JavaScript. Which is actually working correctly.Be yourself. No one else is as qualified.
AdSlot6