vestan Posted July 4, 2009 Report Share Posted July 4, 2009 Hi peeps. B) Just wondering if anyone on here knows much about javascripting?I've gotta setup a program that calculates the first 20 numbers in the fibonacci sequence and stores them in an array.Apparently, this is the very basics that every programmer learns, so any programmers on here should be able to give me some pointers hopefully.I've got most the code written out, but I believe that I must be missing something of vital importance coz I only get the first 2 numbers in the sequence to display in the array.I've been at this for days, have a huge headache with it, any help appreciated muchly. Cheers.Vestan. Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted July 4, 2009 Report Share Posted July 4, 2009 Could you display the current code you're using?It may be how you are making the array variables displayed, it may also be how you are setting up the variables.Instead of displaying them somehow, could you temporarily change it so that they all add up a number and display that number? I used to do that when I started VB .NET to figure out if I had set up my variables properly and it helped quite a bit :)But, in all honesty, I have no idea what the Fibonacci Sequence is. :) Quote Link to comment Share on other sites More sharing options...
vestan Posted July 4, 2009 Author Report Share Posted July 4, 2009 Could you display the current code you're using?It may be how you are making the array variables displayed, it may also be how you are setting up the variables.Instead of displaying them somehow, could you temporarily change it so that they all add up a number and display that number? I used to do that when I started VB .NET to figure out if I had set up my variables properly and it helped quite a bit :)But, in all honesty, I have no idea what the Fibonacci Sequence is. :)Hi. :) The fibonacci sequence is as follows 1 + 1 = 2, 2 + 1 = 3, 3 + 2 = 5, 5 + 3 = 8 etc. etc.The first 20 elemnts in the fibonacci sequence:1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765So my code:<HTML><HEAD><TITLE> Fibonacci sequence</TITLE><script LANGUAGE = "JavaScript">/*program to find the first 20 elements in the fibonacci sequence*///begin by declaring an array where the 20 numbers will be stored//fiboArray = new Array(20);//declare the variables//var fNacci, fNacci1, fNacci2;fNacci = 1;fNacci1 = 1;fNacci = parseFloat(fNacci);fNacci1 = parseFloat(fNacci1);//initialise the first 2 elements in the array//fiboArray[0] = fNacci;fiboArray[1] = fNacci1;//calculate the sequence using a for loop//for (var count = 2; count < fiboArray.length; count = count + 1){ fNacci = fNacci1; fNacci1 = fNacci2; fNacci2 = fNacci + fNacci1;}document.write('The first 20 elements of the Fibonacci sequence are:' + '<BR>' + fiboArray + '<BR>'); </SCRIPT></HEAD><BODY></BODY></HTML>I am unsure as to what it is I'm supposed to do from here??? Cheers.Vestan. Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted July 4, 2009 Report Share Posted July 4, 2009 I haven't used JavaScript before that much so I can't give you a direct answer but can you not make a function to count how many numbers are in the fibonacci sequence and for each number it pushes the array number up?So like,fiboArray[0] = fNacci;fiboArray[1] = fNacci1;and for each number getting counted it goes upfiboArray[2] = fNacci2;fiboArray[3] = fNacci3;etcetera.Because, it looks like you're only including two numbers being stored, counted and used, you're not using anything to progress it.But I'm probably wrong; instead of changing your code around and trying what I suggested you can use this website to save any confusion.Run online JavaScript code Quote Link to comment Share on other sites More sharing options...
vestan Posted July 4, 2009 Author Report Share Posted July 4, 2009 I haven't used JavaScript before that much so I can't give you a direct answer but can you not make a function to count how many numbers are in the fibonacci sequence and for each number it pushes the array number up?So like,fiboArray[0] = fNacci;fiboArray[1] = fNacci1;and for each number getting counted it goes upfiboArray[2] = fNacci2;fiboArray[3] = fNacci3;etcetera.Because, it looks like you're only including two numbers being stored, counted and used, you're not using anything to progress it.But I'm probably wrong; instead of changing your code around and trying what I suggested you can use this website to save any confusion.The calculations for the remaining elements in the array 2 to 20 are supposed to be calculated in the for loop.I'm at a total loss as to how to do this. I'm probably missing something simple, like my brain. :lol: Run online JavaScript codeI'll have another look through my computing books and have a fiddle around with the code on that site you provided the link for.Thanks Hb. :) Vestan. Quote Link to comment Share on other sites More sharing options...
Hb_Kai Posted July 4, 2009 Report Share Posted July 4, 2009 You're welcome. Just found the site myself and quite like it actually. :lol: Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted July 5, 2009 Report Share Posted July 5, 2009 I will look at it later today when I'm fully awake.Meanwhile I am moving the topic to the Programming section of the forum. Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted July 5, 2009 Report Share Posted July 5, 2009 //calculate the sequence using a for loop//for (var count = 2; count < fiboArray.length; count = count + 1){ fNacci = fNacci1; fNacci1 = fNacci2; fNacci2 = fNacci + fNacci1;}You have a nice loop here, but I don't see you putting anything into the fiboArray; e.g.fiboArray[count] = fNacci2;orfiboArray[count] = fNacci + fNacci1;... no wonder your array is empty. Quote Link to comment Share on other sites More sharing options...
vestan Posted July 5, 2009 Author Report Share Posted July 5, 2009 //calculate the sequence using a for loop//for (var count = 2; count < fiboArray.length; count = count + 1){ fNacci = fNacci1; fNacci1 = fNacci2; fNacci2 = fNacci + fNacci1;}You have a nice loop here, but I don't see you putting anything into the fiboArray; e.g.fiboArray[count] = fNacci2;orfiboArray[count] = fNacci + fNacci1;... no wonder your array is empty.I've almost got it working.:)fiboArray = new Array(20);//declare the variables//var fNacci, fNacci1, fNacci2;fNacci = 1;fNacci1 = 1;fNacci2 = 0;fNacci = parseFloat(fNacci);fNacci1 = parseFloat(fNacci1);fNacci2 = parseFloat(fNacci2);//initialise the first 2 elements in the array//fiboArray[0] = fNacci;fiboArray[1] = fNacci1;//calculate the sequence using a for loop//for (var count = 2; count < fiboArray.length; count = count + 1){ fNacci = fNacci1; fNacci1 = fNacci2; fNacci2 = fNacci + fNacci1; fiboArray[count] = fNacci2;}document.write('The first 20 elements of the Fibonacci sequence are:' + '<BR>' + fiboArray + '<BR>');Thanks for the help everyone. :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.