Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:11:30+00:00 2026-06-12T00:11:30+00:00

I am very new to programming in general and am having a hard time

  • 0

I am very new to programming in general and am having a hard time understanding this Fibonacci sequence example:

var fib = [0, 1];
for (var i = 2; i < n; i++) {
    fib[ i ] = fib[ i - 1 ] + fib[ i - 2 ];
    console.log(fib);
}

On the first iteration, index 2 is equal to 1, simple enough. But, when I try the second iteration with i = 3, I get:

fib[ 3 ] = fib[ 3 - 1 ] + fib[ 3 - 2 ];  
fib[ 3 ] = fib[ 2 ] + fib[ 1 ]; 
fib[ 3 ] = fib[ 3 ];

Where am I going wrong with my thinking? So far I have:

var fib = [0,1,1,3]

which I know is not correct.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:11:31+00:00Added an answer on June 12, 2026 at 12:11 am

    When you are reasoning about the code, you make the jump from fib[3] = fib[2] + fib[1] to fib[3] = fib[3]. This happens to be a transformation that results in a correct statement, but it is not how it works. This code is adding the value at index 2 to the value at index 1. That is not the same as taking the value at index 3. The way this reasoning should work is as follows:

    You start with fib = [0, 1]. Then in the first iteration of the loop you have fib[2] = fib[1] + fib[0]. This means that you add the value at index 0 (which happens to be 0) to the value at index 1 (which happens to be 1) to get the value that you put at the end of the array (1). Then in the second iteration, you do a similar thing, adding the value at index 1 (still 1) to the value at index 2 (also 1) to get 2, which goes at the end of the array. This continues, and at each iteration you add together the last two values in the array to get the next value.

    In JavaScript, when using an array like fib, fib[i] refers to the ith value in this array, counting from 0. So fib[0] is the first element in the array, fib[1] is the second element in the array, and so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think This question might be very general...I am new to Serial port programming
I am very new to programming in general, while reading about PHP I saw
I'm very new to Python and multithreaded programming in general. Basically, I have a
I'm very new to jquery and programming in general but I'm still trying to
I am very new to Ruby, and programming in general. Firstly, I have the
I am very new to Python/Django and programming in general. With the limited tools
I am very new to Java (and programming in general, my previous experience is
I'm very new to Android programming and am having a rough start. I've created
I'm very new to storyboarding (also pretty new to iOS programming in general). I'm
I’m very new to programming with RSS feeds to please forgive me if this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.