Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    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:-



    <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>')





    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


  2. #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,703 Times in 2,679 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>')

    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.
    6-4/2 = 4 but (6-4)/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; 06-26-2009 at 08:07 PM.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts