0

I'm trying to loop through an array and check if the User's ip address matches one of the i.p addresses in my clientip array. After the first loop (i=0), it jumps to the else statement right away and doesn't check the other elements in the array. Any idea what is wrong? I think it's my logic that is wrong.

<script>
        $(document).ready(function(){
            for(var i = 0; clientip.length; i++){
                if(clientip[i] === userip.toString() ){
                    console.log("Your IP is :", userip);
                    $("#showButtons").show();
                    break;
                }
                else{
                    console.log("Wrong ip address");
                    console.log(userip);
                    $("#showButtons").hide();
                    alert("You are not connected to the correct IP Address");
                    break;
                };
            };
        });
    </script>

Thank you

1
  • Thanks everyone for your help! Commented Apr 12, 2016 at 19:49

5 Answers 5

2

You should better try indexOf method.

So, if you change your if statement as, if(clientip.indexOf(userip.toString() ) != -1) or if(clientip.indexOf(userip.toString() ) >=0) eveything will be fine. Thus you'll not need break statement.

Sign up to request clarification or add additional context in comments.

Comments

1

You have

break;

In both if and else, so it will always break loop after first try, and for is broken...

Try:

<script>
    $(document).ready(function(){
        for(var i = 0; i < clientip.length; i++){
            if(clientip[i] === userip.toString() ){
                console.log("Your IP is :", userip);
                $("#showButtons").show();
                break;
            }
            else if (i === clientip.length -1) {
                console.log("Wrong ip address");
                console.log(userip);
                $("#showButtons").hide();
                alert("You are not connected to the correct IP Address");
                break;
            };
        };
    });
</script>

5 Comments

i >= clientip.length -1 would be beter.
"i >= clientip.length -1 would be beter." - i will never be greater than clientip.length
@meler I erased because i was wrong and i gave you +1. So why would you give -1.
@DominiqueFortin no hard feelings, everything is right now. Regards
@meler It's a good practice to use >= and <= in for loop condition instead of === or !==.
0

For loop stops after first iteration because of break stetement

2 Comments

I removed it and it still jumps to the else statement after first iteration. i set a breakpoint at the first if statement and it jumps again back down
@krup Well, it jumps to else branch, because your if condition is false. Removing break only solve this thing: doesn't check the other elements in the array. Apparently, it jumps to else branch, because first client ip isn't equals to userip :)
0

why are you using break within your if and else statements? htry removing them

also, you may want to set a flag, lets say the 'found' variable give it the initial value of false and make it true in a if statement, in case the ip is found in your array, then , once the while is done searching, you could use your if / else statement

if(found===true ){
   console.log("Your IP is :", userip);
   $("#showButtons").show();

 }
 else{
   console.log("Wrong ip address");
   console.log(userip);
   $("#showButtons").hide();
   alert("You are not connected to the correct IP Address");
};

otherwise every iteration of your cycle would be shoing the found/ not found message

5 Comments

The breaks are designed to stop the iteration when the desired value is found. Why waste time continuing the iteration if you've already found what you're looking for?
@devlincarnate, yes, but in the question, the asker is using break in the if and the else blocks, which means only the first iteration is ever run. There might as well not be a loop there at all.
@devlincarnate even following that logic, the break should not be present on the else statement, otherwise if the ip is not found in the first position it wont be found, ever.
@ArturoMontaño - so THAT should be your suggestion, not to just remove the breaks.
Perhaps, but using break in that way si against conventions, and his code would be better written if he manages to make the if condition work instead of using breaks
0

clientip.length will always be truthy (unless the array is empty), so you should declare your loop like this:

for(var i = 0; clientip.length < i; i++)

The logic in the rest of your loop is kind of bizzarre. The else clause handles the case of what happens if the IP doesn't match anything in the array, but you can't know that until the loop is complete, so you shouldn't put that in there:

        var found = false;

        for(var i = 0; clientip.length; i++){
                        if(clientip[i] === userip.toString() ){
                            console.log("Your IP is :", userip);
                            $("#showButtons").show();
                            // You found a match.  You're done.
                            found = true
                            break;
                        }

                    }
// No match was found.  
   if(!found) {
       console.log("Wrong ip address");
       console.log(userip);
       $("#showButtons").hide();
       alert("You are not connected to the correct IP Address");
       break;
   }

Finally, don't put semicolons after loops or if/else blocks. At best, they don't do anything, and at worst, they'll cause you code to do something funky.

However, there are easier ways to check an array for the presense of a certain value:

if(clientip.indexOf(userip.toString()) != -1)
{
// userip was found
} else {
// userip was not found
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.