rexlunae wrote:MessiahAndrw wrote:rexlunae wrote:
Anyway, just one more comment: Please tell me you aren't trying to write an OS in Javascript.
Nope, I'm using FASM for my OS. I used Javascript because I was able to quickly implement the function that I had in my head.
So, how is this function supposed to work?
Quite simple really. He didn't post the code right.
MessiahAndrw wrote:
Code: Select all
function squareroot(number)
{
var answer = 0;
if (answer > 10000*10000)
{
answer = 50000;
}
if (answer <= 10000*10000)
{
answer = 5000;
}
if (answer <= 1000*1000)
{
answer = 500;
}
if (answer <= 100*100)
{
answer = 50;
}
if (answer <= 10*10)
{
answer = 5;
}
while (((answer+1)*(answer+1) <number> number))
{
if(answer*answer <number> number)
{
answer = answer - 1;
}
}
return answer;
}
I know that this works because I have seen him run the Javascript version. The function has to be passed "number" by a call in the HTML body of the page it is in:
Obvoiusly the * would be replaced by the number to be square-rooted. if the number is greater than 100000000 then it begins with a value of the square root which is 50000, if less then itgoes through a seris of tests to determine what range the nuber is in. THis is not stricly necessary, since if answer beings at zero it will eventually reach the required value, but is merely part of the optimising MessiahAndrw used. Answer is only set to 5 if the number is less than 100.
The next part of the code is where think there is a mistake, since I think he mis-copied the script fromt he HTML document. I think that if:
is replaced by != then it might work.
so far I have come up with
Code: Select all
<HTML>
<HEAD>
<SCRIPT>
function squareroot(number)
{
var answer = 0;
if (answer > 10000*10000)
{
answer = 50000;
}
if (answer <= 10000*10000)
{
answer = 5000;
}
if (answer <= 1000*1000)
{
answer = 500;
}
if (answer <= 100*100)
{
answer = 50;
}
if (answer <= 10*10)
{
answer = 5;
}
while (((answer+1)*(answer+1) <number> number))
{
if(answer*answer <number> number)
{
answer = answer - 1;
}
}
alert(answer);
}
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT>
var a=prompt("enter the number to square-root","");
squareroot(a);
</SCRIPT>
dskih
</BODY>
</HTML>
to try to get it to work, but It dosent seem to yet.
I will ask messiahAndrw to rep post the entire HTML page, which I know works, and then we can see how he did it. I know this method will always round the answer down, not up.