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 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Coder
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I add the incrementing number of a for loop to an if statement inside the loop

    Here is my code:

    Code:
    	for (var i=2; i<61; i++) {
    	if (h[i] == "Empty") {  single="Z"; }
    	if (h[i] == "Hull") {  single="e"; }
    	if (h[i] == "Bulk") {  single="f"; }
    	if (h[i] == "Master") {  single="g"; 
            }
    I am trying the assign the value of i, so it becomes:

    h2 == "Empty"
    h2 == "Hull"
    h2 == "Bulk"
    h2 == "Master"
    h3 == "Empty"
    h3 == "Hull"
    h3 == "Bulk"
    h3 == "Master"
    h4 == "Empty"
    h4 == "Hull"
    h4 == "Bulk"
    h4 == "Master"
    .... and so on .....

  • #2
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    You generally don't want to override your loop counter. However, I fail to see in which sense the current loop does not do what you want it to do. Can you elaborate (or does anyone else understand)?
    You are also missing a closing brace for the last if statement.

    In any case, you might want to rewrite that if-code to a switch/case statement. Would make it much nicer to look at and read.

  • #3
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Oh, now I get it (I think). Instead of h[2], you want to have h2 -- correct? In that case it would be much better if you didn't create variables h2, h3, ..., but the array h that you are currently referencing. Using 59 variables for what an array is for is very bad practice and leads to problems like these.

  • #4
    New Coder
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the reply Airblader,

    yes the problem is the H[1], H[2], H[3]....

    i have wrote lots of code in the H1, H2, H3.... format now, I will remember next project to use an array instead.

    how do i reference the loop incrementing number inside the if statements?

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,180
    Thanks
    80
    Thanked 4,560 Times in 4,524 Posts
    It's a really really bad idea, but...
    Code:
    var mapping = {
        Empty : "Z",
        Hull : "e",
        Bulk : "f",
        Master : "g"
    }
    
    for (var i=2; i<61; i++) 
    {
        var h = window["h" + i];
        single = mapping[h];
    }
    I improved your 4 if statements into a single object lookup table. But you could use the "ifs" if you really must.
    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.

  • #6
    New Coder
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you OldPenant that works nicely, i like the mapping technique cuts down on a lot of the page text.

    Is it possible to use multidimensional mapping, like:


    Code:
    for (var i=2; i<61; i++) 
    {
    	for (var j=1; j<6; j++) 
    	{
    var mapping = {
        Empty : "Z",
        Hull : "e",
        Bulk : "f",
        Master : "g"
                  }
        var hw = window["h" + i + "w" + j];
        single = mapping[hw];
           }
    }
    It is not working how I coded it above, it is for the variables h1w1, h1w2, h1w3, h1w4 .... etc...

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,180
    Thanks
    80
    Thanked 4,560 Times in 4,524 Posts
    Should work. Dunno why it wouldn't.

    But now it really and truly is time to rewrite your code.

    That's an even more terrible design than the original.

    How long would it take you to change from using h1w1, etc., to h[1][1], etc.??
    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.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,180
    Thanks
    80
    Thanked 4,560 Times in 4,524 Posts
    Here, proof it works:
    Code:
    <script>
    var h1w1 = "Empty", h1w2 = "Hull", h1w3 = "Empty",
        h2w1 = "Master", h2w2 = "Bulk", h2w3 = "Empty",
        h3w1 = "Bulk", h3w2 = "Hull", h3w3 = "Master";
    
    var mapping = {
        Empty : "Z",
        Hull : "e",
        Bulk : "f",
        Master : "g"
    };
    
    for (var i=1; i <= 3; i++) 
    {
        for (var j=1; j <= 3; j++) 
        {
            var hw = window["h" + i + "w" + j];
            single = mapping[hw];
            document.write( "h" + i + "w" + j + " contains " + hw + " and maps to " + single + "<br/>" );
        }
    }
    </script>
    Note: You should put the mapping initialization outside the loop.
    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
    New Coder
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help OldPendant,

    This is where the problem comes in, there is h1w1, h1a1, h1s1, h1t1, and I can't put all the different end letters into one array, as the different numbers of h's have different numbers of w, a ,s, t's and i need to know which letter the values all belong to.

    I been trying a few different variations, but havent got the code to work yet

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,180
    Thanks
    80
    Thanked 4,560 Times in 4,524 Posts
    Truly and really time for a complete redesign. Honest.

    If you explained what the purpose of all this is, you might get some ideas from here.
    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
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by Old Pedant View Post
    Truly and really time for a complete redesign. Honest.
    +1. This code smells. And smelly code needs a rewrite, not more smelly code.

  • #12
    New Coder
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Explain how I am going to put:
    h1, h1w1, h1h2, h1w3, h1a1, h1s1, h1s2,
    h2, h2w1,h2w2, h2a1, h2a2, h2t1,
    h3, h3w1, h3a1
    .... etc .... into arrays

    Notice how the different 'h' have differing total numbers of 'w' 'a' 's' 't' values, and some not have any values

  • #13
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    There are ways, but maybe even better ones than to just solve this. But to know that, we need to know what these things are and what the bigger context is. "h", "w", … are horrible names anyway.

    But, just as a starting example, if they represent simple data, they could be organized as an array of objects.

  • #14
    New Coder
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So the first array is going to obviously be h[].

    but then where do i go for 'w' a' 's' 't' values? which may or may not be present and can be anywhere between 0 and 10 of each value.

  • #15
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    A OOP kind of solution (I'm just a fan of OOP, you could do similar things without it):

    Code:
    function ValuesHolder () {
        var values = [];
    
        this.get = function (index) {
            return values[index];
        }
    
        this.set = function (index, value) {
            values[index] = value;
            return this;
        }
        
        // and whatever else you need
    
        return this;
    }
    
    function ValueObject () {
        this.w = new ValuesHolder();
        this.a = new ValuesHolder();
        this.s = new ValuesHolder();
        this.t = new ValuesHolder();
    
        return this;
    }
    
    // example
    var h = [];
    
    h[0] = new ValueObject();
    h[0].w.set(0, 1);
    h[0].w.set(1, 5);
    
    // this assignment is just a demonstration of your old scheme – obviously you do NOT want to create these variables anymore
    var h0w1 = h[0].w.get(1);
    var h0a0 = h[0].a.get(0);
    
    console.assert(h0w1 === 5);
    console.assert(h0a0 === undefined);
    Again, this might be a lot better than your current way of doing things. But there might still be better solutions, for which we need to know more, though.

    Keep in mind that these names are still horrible, but seeing how I have no clue what your code is for, I can't really make better ones.
    Last edited by Airblader; 07-19-2013 at 12:47 PM.


  •  
    Page 1 of 2 12 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
    •