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 2 FirstFirst 12
Results 16 to 24 of 24
  1. #16
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Which is the same answer I gave you before.

    If you don't *KNOW* if "h1t1" exists, *TEST* for it:
    Code:
    var letters = [ "s", "t", w" ];
    for ( var i = 1; i <= 9; ++i )
    {
        for ( var e = 0; e < letters.length; ++e )
        {
            var test = "h" + i + letters[e]; // e.g., "h1a"
            for ( var n = 1; n <= 9; ++n ) // 
            {
                var val = window[test+n];
                if ( val == null ) break; // exit the loop
                if ( val == "Master" ) {... } 
               etc. 
            }
        }
    }
    But no matter what you think, no matter what you say, this is still a *HORRIBLE* way to code it. "Smelly" isn't strong enough language.

    If you aren't willing to recode it, then it will always be ugly and hard to grow.

    See my post in the other thread for other possible ways to do this.

    Again, you won't tell us what the purpose of all this is, so we are left making suggestions in the dark. And I hate being a mushroom. (Mushrooms are kept in the dark and are fed on s**t. Which is how I feel about these threads.)
    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.

  2. #17
    New Coder
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Mushrooms are also an excellent unique vegetable, the highest in protein content of all vegetables and also have stress relieving properties, and other medicinal benefits.

    Sorry OldPendant I didn't have time to explain my whole code, and posting a link to the page in question wouldn't have helped either most the code is server-side PHP. The PHP also being one of factors why it had to be coded the way I was asking.

    You might not like my code, but I understand it, and more importantly it WORKS, it works fast enough and works cross browser and on all devices I tested.

    I tried all the methods supplied, they all didn't work as I expected, and in the end I had to keep coming back to the original post title asking how to use the incrementing number of a For Loop in a If statement in said loop.

  3. #18
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    james, not that I am trying to convince you to change anything anymore. But there are a few points that need to be clarified because you just seem to have some misconceptions (understandably, since you don't seem to be very experienced or a professional):

    Code:
    The PHP also being one of factors why it had to be coded the way I was asking.
    If the PHP code is also your work, then the PHP design might be just as flawed – which would be even worse. Even if not, there is nothing that can stop you from writing your Javascript the way you want it to and then expose it in a different ("bad") way.
    Poor design decisions on one end never justify poor decisions somewhere else in the code. This is exactly what code smell makes you want to do, though. And in the end it's a spiral that at one point will make it impossible to change anything without breaking ten other things.

    You also seem quite sensitive to the term "code smell"[1]. Maybe you're not familiar with it. It's actually a well-established term "in the biz".

    Code:
    You might not like my code, but I understand it,
    Good. But for how long?
    Sure you understand what you write while you write it, but try reading these things in a few weeks.

    Code:
    and more importantly it WORKS
    No, not "more importantly". You should free yourself from this thought asap. Smelly code that doesn't work will at least give you a reason to improve it; smelly code that does work, however, invites you to keep making everything worse.
    The fact that code works (as expected) should be a consequence of proper implementation, not the product of desperate attempts to somehow hack the code into something that probably does what it's supposed to do.

    Code:
    it works fast enough and works cross browser and on all devices I tested.
    And given a proper implementation, so do the mentioned alternatives here. That is a 100% guarantee.
    Why I'm so sure of it? Because the suggested changes are not functional changes. Names of variables, ultimatively, are irrelevant, so anything that I put into an array I can use just like variables. The difference is readability and maintainability.

    There is a nice little rule in software development, the boy scout rule[2]: Always leave code a little cleaner than the way you found it. In particular this implies not to make bad code worse.

    Bad code, however, rots and keeps rotting with every smelly snippet of code you throw on top of it. This process can kill a project. I should know, I deal with the remains of such a project everyday at work, only that it has 27 million customers.

    All this being said, everyone has to learn. Maybe it's something to keep in mind for your next project, at least. I know how much it pains to make drastic changes. But maybe you'll understand some of what I'm trying to tell you.

    [1] http://en.wikipedia.org/wiki/Code_smell
    [2] http://www.informit.com/articles/art...35624&seqNum=6
    Last edited by Airblader; 07-20-2013 at 02:10 AM.

  4. #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
    Quote Originally Posted by james909 View Post
    You might not like my code, but I understand it, and more importantly it WORKS
    Seems to me that these two threads are proof that it does *NOT* work.

    Maybe it worked up to this point, up to the place where you needed to so something it wasn't designed to do. But clearly it is no longer working.

    And I refuse to believe that your PHP code *FORCES* you to use
    Code:
       h1w1 = "Master";
    in your JavaScript when you could have use one of the other suggestions, such as
    Code:
       hvals.h1.w1 = "Master";
    Heck, for that matter, even
    Code:
        h1.w1 = "Master";
    would be better.
    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.

  5. #20
    New Coder
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The whole point of using Arrays is when data is of similar structure, my data is not, the only thing constant is h1,h2,h3,h4, etc.. everything that comes after this is different from each.

    You like Airblader are out to prove me wrong in every way, and are presuming way too much you only know a tiny tiny amount of my code,

    and to be honest I don't why you are even still continuing to reply after the original post question was eventually answered (yes after lots this way is better, that way is better.... all of these I tried and NONE worked)

    This is my last post on this forum, I asked a question and got the replys to other questions, and telling me my question is wrong and I should be asking for this..... instead?

    Recieving massive long replys, all of which I tried, and which didnt work when the simple answer all along was:

    Code:
    window["hull" + i]
    good bye!

  6. #21
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Some people just don't get it. I guess this is the right moment to say: Go on and continue writing that pile of crap code, if you absolutely want to deliver poor quality.

    Makes professionals who know what they're doing look better and saves their jobs. So thank you!

  7. #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
    What's really amazing that he thought I was talking about arrays. Clearly has no idea how objects work in JavaScript.

    He also clearly doesn't get that using window["hull"+i] is something left for a last resort. The worst possible answer (well, other than eval( ).) So sad. But good for the pro programmers, as you said.
    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. #23
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    The annoying thing is that I think we all know I'm not very patient when it comes to ignorance, but here, after being called troll and what not, I pulled myself together and tried with a detailed, friendly explanation, but still no sign of improvement.

    Saying he doesn't know how objects in Javascript work is an understatement. He has clearly no idea about fundamental code design and quality, let alone the languages he is trying to work with. And these are the only reasons the other solutions don't work, but except him everyone here knows that anyway.

  9. #24
    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 Airblader View Post
    The annoying thing is that I think we all know I'm not very patient when it comes to ignorance
    Oh, say it isn't so! You are the model of patience. <grin/>

    I tolerate ignorance reasonably well. I don't tolerate stupidity well. And at first I thought he was merely ignorant.
    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.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

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