0

        var firstNum = 1;
        var secondNum = 2;
        var fibonacciNum;
        var myArray = [];

        for (var k = 3; k <=15; k++) {

            fibonacciNum = firstNum + secondNum;
            firstNum = secondNum;
            secondNum = fibonacciNum;
            
            myArray.push(fibonacciNum);

        }

im trying to push the numbers from the loop into myArray and later putting each number as a list item in an unordered list

6
  • Did you look at the documentation for Array.push ? Commented Mar 10, 2015 at 16:23
  • 1
    myArray.push(fibonacciNum) Commented Mar 10, 2015 at 16:25
  • there is a better algorithm to provide Fibonacci numbers (using matrix multiplication) Commented Mar 10, 2015 at 16:26
  • im still new to javascript, so i understnd it this way. but how then do i later put the array values into an unordered list Commented Mar 10, 2015 at 16:31
  • check this: w3schools.com/js/js_array_methods.asp Commented Mar 10, 2015 at 16:34

2 Answers 2

0

Replace

//fibonacciNum.push(myArray.length);

with

myArray.push(fibonacciNum);
Sign up to request clarification or add additional context in comments.

Comments

0

So, assuming you want to add a new Unordered list to the DOM: (this will work for any array...)

function addUl(arr) {
    //A <ul> is created and appended to the DOM
    var ul = document.createElement("ul");
    ul.setAttribute("id", "list");
    document.body.appendChild(ul);

    var li;
    var element;
    //A <li> is created and filled with an element each time
    //the element will change to the next array element every lap (a)
    arr.forEach(function (a) {
        li = document.createElement("li");
        element = document.createTextNode(a);
        li.appendChild(element);
        document.getElementById("list").appendChild(li);
    });
}

here you can se how it works: example

By the way, if you use jQuery your life will be easier...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.