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

Results 1 to 2 of 2
Thread: please help me really stuck

06262009, 07:03 PM #1
 Join Date
 Jun 2009
 Posts
 1
 Thanks
 0
 Thanked 0 Times in 0 Posts
please help me really stuck
Hello All,
please could you help me as I am pulling my hair out with this one please could you look at my code and see where I am going wrong I am trying to write a program with the following :
create an Array called fibArray which generates the first 20 fibonacci numbers
Then it should calculate the ratio by dividing each element of the first Array with the one before it and store these values in a second parrallel array called ratioArray
It should then find the first value of ratioArray that is closer to the golden ratio than 0.001m byt.
1.61803399 will be used as the value for the golden ratio.
finally it should display output in browser.
This is what I have currently:
<html>
<body>
<script type="text/javascript">
var fibArray = new Array ();
document.write("<h4>The first 20 elements of the sequence are:</h4>");
for (i=0, j=1, k=0, fibArray =0; i<21; i++, fibArray=j+k, j=k, k=fibArray){
document.write(fibArray + ' ,');
}
for (i = 0; i < myfibArray.length; i++) document.write (myfibArray[i], '  ', myfibArray[i] / 2, '<br>')
for (i = 0, j = 0; i < myfibArray.length; i++, j++) if (myfibArray[i] / 2 == 1.5)
document.write ('Line ', j, ' presents the value 1.5<br>')
var ratioArray= new Array();
for(i=0;i<fibArray.length;i++) {
var calculation = (myfibArray[i], '  ', myfibArray[i] / 2, '<br>')
sequence.push(calculation);
}
</script>
</HEAD>
<BODY>
</BODY>
</HTML>
Please can someone help I have been trying to do this every night this week and am loosing hope that I will ever learn this language. Any help will be greatly appreciated.
Thankyou in advance
Sarah
06262009, 07:15 PM
#2
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,308
 Thanks
 203
 Thanked 2,563 Times in 2,541 Posts
A good deal of the trouble is that your array is
var fibArray = new Array ();
but you refer to it in several places as myfibArray
for (i = 0, j = 0; i < myfibArray.length; i++, j++) is not valid.
You must write a nested loop as
for (var i = 0; i <fibArray.length; i++) {
for (var j = 0; j <fibArray.length; j++) {
///.......
} end of j loop
} end of i loop
But your second loop is redundant.
for (i = 0; i < fibArray.length; i++) {
if ((fibArray[i] / 2) == 1.5) {
document.write ('Array element " + i + " presents the value 1.5<br>"); // but where does the value 1.5 come in???
}
}
[I]var calculation = (myfibArray[i], '  ', myfibArray / 2, '<br>')
sequence.push(calculation);
No idea what this is supposed to do. You cannot combine a mathematical calculation with a string.
You need to have a look at order of precedence of operations and use of brackets.
64/2 = 4 but (64)/2 = 1
Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. Why not edit it to something meaningful such as Fibonacci Numbers And Golden Ratio.
All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
Last edited by Philip M; 06262009 at 08:07 PM.