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 24
  1. #1
    New Coder
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Does anyone know how to use the incrementing number of a For Loop in a If statement

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

    Here is the code:

    Code:
    	for (var i=2; i<21; i++) {
    	if (hulli == "Empty") {
           // do this }
    	if (hulli == "Property") {
           // do this }
            }
    How do I include the value of i into the variable I am checking to make the statement check variables 'hull1', hull2', 'hull3' etc

  • #2
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts

  • #3
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Why don't you change hull1, hull2, hull3 to array, it will work better then old one.

    Code:
    var Hull = [hull1, hull2, hull3]; //if you don't want to change all of your code so this is easy way to do it.
    
    // Need to minus 1 since array start from 0 but if your hull start from 0 then the minus 1 is no need.
    // You can use "var i = 1; i < 20;" insert what it wrote down here.
    for (var i = 2 - 1; i < 21 - 1; i++) {
        if (Hull[i]) == "Empty") {}
        if (Hull[i]) == "Property") {}
    }

  • #4
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    @ Richter

    Please look into his other thread that I linked. He has both been given an answer to do "what he wants" and how to do it different. This is pretty much an exact copy of his question.

  • #5
    New Coder
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    An array won't work due to problems I explained on my other thread.

    I need to know how to add the value of i into the variable i'm checking in the if statements

  • #6
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Thanks Airblader, when the time I wrote an answer, I didn't see you post until I post my answer (this webboard hasn't feature that alert a new post before user summit his post).

    @james909,
    Why do you have to split into new topic since it relate with your previous question ?
    Now you are the one that make me confuse.
    Last edited by Richter; 07-19-2013 at 02:08 PM.

  • #7
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    And you got that answer in the other topic. It's still not very nice to say you will change it, but then just open a second thread with the same question.

    I'm out of here.

  • #8
    New Coder
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You answer was to totally rewrite to something I don't understand, after calling my code smelly, when you don't even understand why I am wanting it in this format and NOT in an array. My code will not work in an array so forget it, I am looking for the answer of the original question: Does anyone know how to use the incrementing number of a For Loop to check the value of a variable in an If statement.

    I have tried your answer and it simply won't work in my code, and instead of answering what I am asking you are just trolling my posts, the sooner you stop posting on my threads the better!

    I doubt anyone here is going to answer my original question, I am dubious about this whole forum, I have received 2 private messages in the past 24 hours asking me to go to another website to have my coding questions answered (oh but when you get there you find out it will costs to ask questions).

  • #9
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Code smell is a fact and I'm not going to go on trying to convince you that you can always use an array instead – whatever. It doesn't change the fact that your original question has been answered in the other thread.

    You just didn't read it carefully.

  • #10
    New Coder
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I read the whole thread many times, your not understanding the question, I want to use For Loops with If statements inside

    The question is: Does anyone know how to use the incrementing number of a For Loop to check the value of a variable in an If statement that us inside said For loop?

    And the answer is..............

  • #11
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    @james909, I don't know why you can't use an array. Life would be simpler. But to answer your question: you can concatenate two STRINGS, but that is not the same as hull1 being equal to "Empty". But we can get there.
    To concatenate : "hull".concat(i)
    Code that might help:
    Code:
    <script type="text/javascript">
    for (i=2; i<21; i++) {
    	switch ("hull".concat(i)){
    		case "hull2":
    			Put the 'Do This code for "Empty" Here'
    			break;
    		case "hull3":
    			Put the 'Do This code for "Property" Here'
    			break;
    	}
    }
    </script>
    Evolution - The non-random survival of random variants.

  • #12
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    The answer is in post #5 by Old Pedant (in the other thread)

  • #13
    New Coder
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes Airblader troll, and you read my reply below(#6) that works for creating h1, h2, etc.., although it doesn't work in the way I need it to by creating h1w1, h1a1, etc.. for this i need to know how to: USE THE INCREMENTING NUMBER OF A FOR LOOP TO CHECK THE VALUE OF A VARIABLE IN AN IF STATEMENT INSIDE SAID FOR LOOP

    all i am getting is use this method instead, its better because....

    when it actually isn't because you don't know my whole code, and why I specifically need to use the a for loop with if statements to create the variables

  • #14
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by james909 View Post
    when it actually isn't because you don't know my whole code, and why I specifically need to use the a for loop with if statements to create the variables
    Which just suggests flawed design, nothing else. You certainly aren't working on something that breaks code architecture techniques that have been used for years in multi million LoC projects.
    Of course, a rewrite hurts. But the more you push it away, the more it will hurt. It starts with problems like these (and you should be happy Javascript offers you a solution many languages wouldn't) and ends in total chaos.

    Anyway since you don't care about any of this, I'll try to be even more specific (though I thought pointing out the post should suffice):

    Code:
    window["hull" + i]

  • #15
    New Coder
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    THANK YOU AIRBLADER

    the:
    Code:
    window["hull" + i]
    code works perfectly as I need it, now I am happy to make my smelly code just how I need it


  •  
    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
    •