Jump to content

JavaScripting help required.


Recommended Posts

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. :flowers:

Cheers.

Vestan.

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

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,6765

So 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??? :unsure:

Cheers.

Vestan.

Link to comment
Share on other sites

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 up

fiboArray[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

Link to comment
Share on other sites

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 up

fiboArray[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. :unsure:

I'm probably missing something simple, like my brain. :lol:

I'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.

Link to comment
Share on other sites

//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;

or

fiboArray[count] = fNacci + fNacci1;

... no wonder your array is empty.

Link to comment
Share on other sites

//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;

or

fiboArray[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. :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy