Results 1 to 9 of 9

Thread: Help Need with Javascript

  1. #1
    Join Date
    May 2010
    Posts
    3,851
    Rep Power
    17

    Exclamation Help Need with Javascript

    Need help writing an array in Javascript.


    I need to create an array of size 20, in order to populate the array i have to do some addition.

    The values in slots [0] and [1] will be added and saved in slot [2], then slot [1] and [2] will be added to fill slot [3].

    This is what I have so far

    Code:
    <script type="text/javascript">
    <!--
    var myArray = new Array(20);
    
    myArray[0] = "2";
    myArray[1] = "1";
     
     for (var i = 0; i < myArray.length-1; i++)
    {
       for (var j = 1; j < myArray.length-1; j++)
    
       {
         for (var x = 2; x < myArray.length; x++)
    
    {
         myArray[x] = myArray[i] + myArray[j];
    
    
    document.write( +myArray[x] );
    }
    }
    }
    //-->
    </script>
    Last edited by Arch_Angel; Feb 17, 2012 at 01:11 AM. Reason: added CODE tags

  2. #2
    Join Date
    Apr 2003
    Posts
    13,269
    Rep Power
    34

    Default

    Haven't tested this and I haven't coded in javascript in a while so I am a bit rusty. But see if this will work.
    Code:
    <script type="text/javascript">
    <!--
    var myArray = new Array(20);
    
    myArray[0] = "2";
    myArray[1] = "1";
    var i = 0;
    var j = 1;
    for (var x = 2; x < myArray.length; x++)
    {
        myArray[x] = myArray[i] + myArray[j];
        document.write(myArray[x]);
        i++;
        j++;
    }
    
    //-->
    </script>
    Last edited by Arch_Angel; Feb 17, 2012 at 01:33 AM.
    "The best software is the one that fits your needs." - A_A

    Virus free since: date unknown
    Anti-virus free since: August 2008

  3. #3
    Join Date
    May 2010
    Posts
    3,851
    Rep Power
    17

    Default

    Quote Originally Posted by Arch_Angel View Post
    Haven't tested this and I haven't coded in javascript in a while so I am a bit rusty. But see if this will work.
    Code:
    <script type="text/javascript">
    <!--
    var myArray = new Array(20);
    
    myArray[0] = "2";
    myArray[1] = "1";
    var i = 0;
    var j = 1;
    for (var x = 2; x < myArray.length; x++)
    {
        myArray[x] = myArray[i] + myArray[j];
        document.write(myArray[x]);
        i++;
        j++;
    }
    
    //-->
    </script>

    just tried it but only gives a repetition of 21121211211212112121121121211211212112121121121211 21211211212112112121121211211212112112121121211211 212


    http://writecodeonline.com/javascript/

  4. #4
    Join Date
    Apr 2003
    Posts
    13,269
    Rep Power
    34

    Default

    Quote Originally Posted by King_Jay16 View Post
    just tried it but only gives a repetition of 21121211211212112121121121211211212112121121121211 21211211212112112121121211211212112112121121211211 212


    http://writecodeonline.com/javascript/
    Oops. Remove the quotes around the 2 and the 1. What javascript does with the values depends on how you store the values.
    You stored them as strings (the quotes around them), so when you try to add them, javascript puts them together to get 21.

    Remove the quotes and it will treat them as numbers.

    Code:
    <script type="text/javascript">
    <!--
    var myArray = new Array(20);
    
    myArray[0] = 2;
    myArray[1] = 1;
    var i = 0;
    var j = 1;
    for (var x = 2; x < myArray.length; x++)
    {
        myArray[x] = myArray[i] + myArray[j];
        document.write(myArray[x]);
        i++;
        j++;
    }
    
    //-->
    </script>
    "The best software is the one that fits your needs." - A_A

    Virus free since: date unknown
    Anti-virus free since: August 2008

  5. #5
    Join Date
    May 2010
    Posts
    3,851
    Rep Power
    17

    Default

    Quote Originally Posted by Arch_Angel View Post
    Oops. Remove the quotes around the 2 and the 1. What javascript does with the values depends on how you store the values.
    You stored them as strings (the quotes around them), so when you try to add them, javascript puts them together to get 21.

    Remove the quotes and it will treat them as numbers.

    Code:
    <script type="text/javascript">
    <!--
    var myArray = new Array(20);
    
    myArray[0] = 2;
    myArray[1] = 1;
    var i = 0;
    var j = 1;
    for (var x = 2; x < myArray.length; x++)
    {
        myArray[x] = myArray[i] + myArray[j];
        document.write(myArray[x]);
        i++;
        j++;
    }
    
    //-->
    </script>
    Thanks!!!


    i figured out another way as well


    Code:
    <script type="text/javascript"> 
    <!--
    var myArray = new Array(20);
    
    myArray[0] = "2";
    myArray[1] = "1";
     
    
    for (var i = 2; i < myArray.length-1; i++)
    {
    
       myArray[i] = parseInt(myArray[i-1]) + parseInt(myArray[i-2]);
    
       document.write( +myArray[i] + ','  );
    }
    //-->
    </script>


    Thanks again... first time with Javascript so was aplying C/C++/C# knowledge...



    OT: We need a rep system here!!!

  6. #6
    Join Date
    Apr 2003
    Posts
    13,269
    Rep Power
    34

    Default

    Quote Originally Posted by King_Jay16 View Post
    Thanks!!!

    i figured out another way as well
    Yes that will work, but you're making extra calls to the function parseInt(), to convert the string to an integer.
    Instead, just remove the quotes around the 2 and the 1 and you wouldn't have to use parseInt(). Javascript will treat it as an integer.

    Code:
    myArray[0] = 2;
    myArray[1] = 1;
    ...
    ...
    myArray[i] = myArray[i-1] + myArray[i-2];
    ...
    In a small script like this, the small overhead of extra calls to parseInt() when it's not needed, won't make a huge difference. But better start coding efficiently now instead of later when your page starts to take some time to load.
    "The best software is the one that fits your needs." - A_A

    Virus free since: date unknown
    Anti-virus free since: August 2008

  7. #7
    Join Date
    May 2010
    Posts
    3,851
    Rep Power
    17

    Default

    Quote Originally Posted by Arch_Angel View Post
    Yes that will work, but you're making extra calls to the function parseInt(), to convert the string to an integer.
    Instead, just remove the quotes around the 2 and the 1 and you wouldn't have to use parseInt(). Javascript will treat it as an integer.

    Code:
    myArray[0] = 2;
    myArray[1] = 1;
    ...
    ...
    myArray[i] = myArray[i-1] + myArray[i-2];
    ...
    In a small script like this, the small overhead of extra calls to parseInt() when it's not needed, won't make a huge difference. But better start coding efficiently now instead of later when your page starts to take some time to load.

    i agree with you on that, thank you!!

  8. #8
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Fibonacci sequence

    Looks like you're trying to generate Fibonacci's Sequence. You could try the following code I cobbled together. Its a cleaner implementation with the algorithm contained within a function:
    Code:
    // define Fibonacci function    
    function fibonacci(length) {
        if (length < 2) throw new Error("length must be greater than or equal to 2");
        
        var sequence = [1,1]; // declare and initialize an array
        var i = 2;
        
        while(i < length){
            sequence.push(sequence[i-2] + sequence[i-1]);
            i++;
        }
        
        return sequence;
    }
        
    // generate an array with the first 20 numbers in the Fibonacci sequence
    var fib_20 = fibonacci(20);
    document.write(fib_20);
    I posted this code up on jsdo.it. So you can try it out for yourself at the following link : Fibonacci Sequence
    Last edited by xegnma; Feb 20, 2012 at 08:25 PM.

  9. #9
    Join Date
    May 2010
    Posts
    3,851
    Rep Power
    17

    Default

    Quote Originally Posted by xegnma View Post
    Looks like you're trying to generate Fibonacci's Sequence. You could try the following code I cobbled together. Its a cleaner implementation with the algorithm contained within a function:
    Code:
    // define Fibonacci function    
    function fibonacci(length) {
        if (length < 2) throw new Error("length must be greater than or equal to 2");
        
        var sequence = [1,1]; // declare and initialize an array
        var i = 2;
        
        while(i < length){
            sequence.push(sequence[i-2] + sequence[i-1]);
            i++;
        }
        
        return sequence;
    }
        
    // generate an array with the first 20 numbers in the Fibonacci sequence
    var fib_20 = fibonacci(20);
    document.write(fib_20);
    I posted this code up on jsdo.it. So you can try it out for yourself at the following link : Fibonacci Sequence
    Yup that is what I am generating, thanks for the additional post

Posting Permissions

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