Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 38
  1. #16
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Talk about my code ?
    Any object input as ID will be convert to integer if it can since I already convert it at code "+ID".
    Last edited by Richter; 07-31-2013 at 06:37 PM.

  2. Users who have thanked Richter for this post:

    martynball (07-31-2013)

  3. #17
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I *STILL* say THIS CODE MAKES NO SENSE!!!
    Code:
    function createArray(num) {
            alert(num);
    	//Has the array been made?
    	if (!sales[num]) 
    	{
    		//Create the array and populate with products, setting them to 0
    		for (x in products) 
    		{
    			temp[x] = 0;
    			sales[num] = temp;
    		}
    	}
    }
    (1) The temp in here is POINTLESS.
    (2) If there is more than one value in products, ONLY THE LAST ONE will be stored in sales[num].

    Try this:
    Code:
    function createArray(num) {
            alert(num);
    	//Has the array been made?
    	if (!sales[num]) 
    	{
    		//Create the array and populate with products, setting them to 0
    		for (x in products) 
    		{
    			sales[num][x] = 0;
    		}
    	}
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  4. Users who have thanked Old Pedant for this post:

    martynball (07-31-2013)

  5. #18
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Old Pedant View Post
    I *STILL* say THIS CODE MAKES NO SENSE!!!
    Code:
    function createArray(num) {
            alert(num);
    	//Has the array been made?
    	if (!sales[num]) 
    	{
    		//Create the array and populate with products, setting them to 0
    		for (x in products) 
    		{
    			temp[x] = 0;
    			sales[num] = temp;
    		}
    	}
    }
    (1) The temp in here is POINTLESS.
    (2) If there is more than one value in products, ONLY THE LAST ONE will be stored in sales[num].

    Try this:
    Code:
    function createArray(num) {
            alert(num);
    	//Has the array been made?
    	if (!sales[num]) 
    	{
    		//Create the array and populate with products, setting them to 0
    		for (x in products) 
    		{
    			sales[num][x] = 0;
    		}
    	}
    }
    Why am i getting this:
    Unable to set property "KR" of undefined or null reference

  6. #19
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Sorry...we all missed one important step.

    Working demonstration:
    Code:
    <script type="text/javascript">
    var sales = [ ];
    
    var products = {
        "AA" : 47,
        "QQ" : 91,
        "ZX" : 7
    };
    
    function createArray(num) 
    {
        num = "N" + num; // see comments below
        if (!sales[num]) 
        {
            //Create the array and populate with products, setting them to 0
            sales[num] = [ ];  // we all missed this!
            for (x in products) 
            {
                sales[num][x] = products[x];
                // ** OR ** you can of course do:
                // sales[num][x] = 0;
            }
        }
    }
    
    createArray( 4 );
    createArray( 2 );
    createArray( 4 );
    
    // dump out contents of sales:
    for ( var s in sales )
    {
        document.write( s + " : {" );
        for ( var ss in sales[s] )
        {
            document.write( ss + ":" + sales[s][ss] + "," );
        }
        document.write("}<br/>");
    }    
    
    </script>
    As we keep saying you do *NOT* really want an array. You want a network of objects.

    But if you create sales[num] you will have an array. But if it doesn't start at 0 and increment from there, it will be a "sparse array" and difficult to work with.

    Better to use a *STRING* here, as I have done, so you have have sales["N50"] (or whatever).

    And then it's easy to iterate through that, as I have done in the code that dumps out the "sales" contents.

    The above code *WORKS*, at least the way I expected it to.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  7. #20
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    @Old Pedant #17, I think you have much knowledge and miss a point in abnormal thing, a temp var in his code is what he try to create an object to contain those property he want (those "KR" thing).

    I fix his code and it should work now.
    Code:
    function createArray(num) {
        alert(num);
        //Has the array been made?
        if (!sales[num]) {
            //Create the array and populate with products, setting them to 0
            var temp = {};
            for (x in products) temp[x] = 0;
            sales[num] = temp;
        }
    }
    Last edited by Richter; 07-31-2013 at 08:10 PM.

  8. #21
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Here. A slightly prettier version of the "dump" part of that code.
    Doesn't affect the rest of the code.
    Code:
    // dump out contents of sales:
    document.write( "sales : {<br/>" );
    for ( var s in sales )
    {
        document.write( "&nbsp;&nbsp;&nbsp;&nbsp;" + s + "{" );
        for ( var ss in sales[s] )
        {
            document.write( ss + ":" + sales[s][ss] + "," );
        }
        document.write("}<br/>");
    }    
    document.write("}<hr/>");
    And you might also add it some simple tests, such as:
    Code:
    createArray( 4 );
    document.write( 'sales["N4"]["AA"] = ' + sales["N4"]["AA"] + "<hr/>" );
    createArray( 2 );
    document.write( 'sales["N2"]["ZX"] = ' + sales["N2"]["ZX"] + "<hr/>" );
    createArray( 4 );
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  9. #22
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by Richter View Post
    @Old Pedant #17, I think you have much knowledge and miss a point in abnormal thing, a temp var in his code is what he try to create an object to contain those property he want (those "KR" thing).

    I fix his code and it should work now.
    Code:
    function createArray(num) {
        alert(num);
        //Has the array been made?
        if (!sales[num]) {
            //Create the array and populate with products, setting them to 0
            var temp = {};
            for (x in products) temp[x] = 0;
            sales[num] = temp;
        }
    }
    Yes, that works. But the temp is absolutely *NOT* needed.

    You can replace
    Code:
            var temp = {};
            for (x in products) temp[x] = 0;
            sales[num] = temp;
    with
    Code:
            sales[num] = {}; // or use [ ] as I did...doesn't matter
            for (x in products) sales[num][x] = 0;
    The temp is adding nothing to the effects of the code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  10. #23
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    And just to show you *can* use a sparse array for sales[ ], even though I wouldn't recommend it:
    Code:
    <script type="text/javascript">
    var sales = [ ];
    
    var products = {
        "AA" : 47,
        "QQ" : 91,
        "ZX" : 7
    };
    
    function createArray(num) 
    {
        if (!sales[num]) 
        {
            //Create the array and populate with products, setting them to 0
            sales[num] = [ ];
            for (x in products) 
            {
                sales[num][x] = products[x];
                // ** OR ** you can of course do:
                // sales[num][x] = 0;
            }
        }
    }
    
    createArray( 4 );
    document.write( 'sales[4]["AA"] = ' + sales[4]["AA"] + "<hr/>" );
    createArray( 2 );
    document.write( 'sales[2]["ZX"] = ' + sales[2]["ZX"] + "<hr/>" );
    createArray( 4 );
    
    // dump out contents of sales:
    for ( var n = 0; n < 1000; ++n ) // or whatever range you expect to find array elements
    {
        if ( sales[n] != null ) /* only dump out the non-empty array elements */
        {
            document.write( "sales[" + n + "] = {" );
            for ( var ss in sales[n] )
            {
                document.write( ss + ":" + sales[n][ss] + "," );
            }
            document.write("}<br/>");
        }
    }    
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  11. #24
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    I make it better by use a class and construct object from it that make coder can check type later at #13 but it doesn't seem like he interest it at all.

  12. Users who have thanked Richter for this post:

    martynball (07-31-2013)

  13. #25
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by Richter View Post
    I make it better by use a class and construct object from it that make coder can check type later at #13 but it doesn't seem like he interest it at all.
    Agreed.

    I think the whole fact that he still thinks of this as "arrays" is a sign that he isn't really ready to take the next step.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  14. Users who have thanked Old Pedant for this post:

    martynball (07-31-2013)

  15. #26
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    It can't be help since most people come here to fix their code, not to learn a new code, even you away give them a code that more efficient, they still prefer their old code if it's beyond his knowledge to understand that code and I saw many topic you post very efficient code but they're not ready for a new level.

  16. Users who have thanked Richter for this post:

    martynball (07-31-2013)

  17. #27
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    The only reason I just wanted to get the function I had working is because it looked like it did the job as it was working fine at first.

    I do want to learn more advanced code, so I will look into using a network of objects.

  18. #28
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by martynball View Post
    I do want to learn more advanced code, so I will look into using a network of objects.
    You *HAVE* a network of objects, if you just use "N50" instead of the number 50 as the "index" for the sales[ ] variable.

    But I *DID* post code to show that sales[ ] *CAN* be an array. It's just that, unless you use sales[0], sales[1], etc., in order, you will have a sparse array. That's okay, but it's harder, then, to find all the sparse elements. You have to use the kind of loop code I showed.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  19. Users who have thanked Old Pedant for this post:

    martynball (07-31-2013)

  20. #29
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    I think Object or JSON is better then Array in string index case, Array type should work with integer index more then string index that create a new member to array.
    Last edited by Richter; 07-31-2013 at 10:27 PM. Reason: typo

  21. Users who have thanked Richter for this post:

    martynball (07-31-2013)

  22. #30
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Alright, seems like it all works, but what the heck is going on here:

    Lets say I create it manually like so

    sales["N712"] = [];
    sales["N712"]["LR"] = 0;


    Now below that code I make an alert

    alert(sales["N712"]["LR"]);


    But then I get this error
    Code:
    Unable to get property "N712" of undefined or null reference
    Referencing the alert.


 
Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

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