any javascript expert out there? help please!

T

transkawa

I wrote the script below. I know it runs well but it does not run on
javascript or is there something about javascript i am missing out?
safari reports slow script and does not give me any error report aside
from that. Firefox tells me that there is error on the formula function
which is not true or is that where the script starts to get slow?
need help. just want to know what about javascript and its use on html
environments i don't know about.
xnt

/*
* this program demonstrates the half-interval method of looking for
* the roots of functions i.e where f(x)=0. it takes two points of
different
* signs and then by repeated averaging of their intervals comes to a
very
* close point where x is or very close to 0 by 0.01, i.e the tolerance
for
* deviation from 0.
*/
//global variables
var positive =0;
var negative =0;
var result =0;
function halfInterval(ap, bp){
var temp1 = formula(ap);
var mebug = isZero(temp1);
if(mebug == 'true'){
result = temp1;
return result;
}
var temp2 = formula(bp);
mebug = isZero(temp2);
if(mebug == 'true'){
result = temp2;
return result;
}
var apositive = isPositive(temp1);
var anegative = isNegative(temp1);
var bpositive = isPositive(temp2);
var bnegative = isNegative(temp2);
if ((apositive == true) && (bpositive == 'true')){
if ((anegative == 'true') && (bnegative == 'true')){
return 'both values are of same sign; error.';
}
}

if(apositive == 'true'){
positive = ap;
negative = bp;
}
else{
positive = bp;
negative = ap;
}
result = search(positive, negative);
return result;
}
function search(positive, negative){
var got = 'false';
var midpointX, midpointY;
while(got == 'false'){
midpointX = (positive + negative) / 2;
midpointY = formula(midpointX);
//check for zero tolerance
var zeroIn = isZero(midpointY);
if(zeroIn == 'true'){
got = 'true'; //out of the loop
}
//check for the sign of midpoint
zeroIn = isPositive(midpointY);
//now change loop variables
if (zeroIn == 'true'){
positive = midpointX;
}else {
negative = midpointX;
}
}
return midpointX;
}

function isPositive(x){
return (x > 0);
}
function isNegative(x){
return (x < 0);
}
function isZero(x){
var res = (!(Math.abs(x) > 0.01));
return res;
}
function formula(x){
return (3*x*x - 27); //firefox 3.6 tells me the error is here
}

function checkFeat(){
try{
//function to check now is 3x(squared) - 27. zero is 9
var apoint = parseInt(prompt('input the first x value'));
var bpoint = parseInt(prompt('input the second x value'));
//call half interval function
var ops = halfInterval(apoint, bpoint);
//return result to three decimal places
document.getElementById('res').value = ops;
}
catch(err){
alert('error report: '+ err.toString());
}
}
the html:
<body>
<form action="http://www.example.com/" id="form1">
<input type="button" id="but1" name="but1" value="click this
button!"
onclick="checkFeat();"><br />
<label for="res">Result:</label>
<input type="text" size="20" id="res" >

</form>

</body>
thanks for all your reponses. TFAYR.
xnt
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
I wrote the script below. I know it runs well but it does not run on
javascript or is there something about javascript i am missing out?
safari reports slow script and does not give me any error report aside
from that. Firefox tells me that there is error on the formula function
which is not true or is that where the script starts to get slow?
need help. just want to know what about javascript and its use on html
environments i don't know about.

Your code seems a lot longer than the code for
* close point where x is or very close to 0 by 0.01, i.e the tolerance

Perhaps better to use a variable limit, or execute until there is no
improvement

if(mebug == 'true'){

Don't use 'true' as a Boolean; use true, and don't use ==true as there
is no need.

if ((apositive == true) && (bpositive == 'true')){
if ((anegative == 'true') && (bnegative == 'true')){
return 'both values are of same sign; error.';

More easily done with an XOR operation, or by multiplying temp1 & temp2
and checking the sign.

var apoint = parseInt(prompt('input the first x value'));

Function parseInt should for safety be given an explicit second
argument; but it is better to use a unary + - read the newsgroup FAQ.
 
T

transkawa

In comp.lang.javascript message <[email protected]


Your code seems a lot longer than the code for


Perhaps better to use a variable limit, or execute until there is no
improvement



Don't use 'true' as a Boolean; use true, and don't use ==true as there
is no need.



More easily done with an XOR operation, or by multiplying temp1 & temp2
and checking the sign.



Function parseInt should for safety be given an explicit second
argument; but it is better to use a unary + - read the newsgroup FAQ.

thanks for your input Dr. Stockton. noted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top