2

Is it possible to do something like this in JavaScript?

if (name == 'foo') {
    exampleFunction('some_arg');
}
else if (name == 'bar') {
    exampleFunction('another_arg');
}
for (i in exampleObject) {
    else if (name == exampleObject[i].name) {
        exampleFunction(exampleObject[i].arg);
    }
}
else {
    exampleFunction('default')
}

I tried it, but got an "unexpected keyword else on line 8" (the "else if" within the for loop). Is there another way to do this?

edit: updated this to use exampleObject[i] in the loop. My bad!

2
  • 1
    Why do you even have a for loop if you never use your iterator, i? Commented Mar 3, 2011 at 19:13
  • What are you trying to do, exactly? Commented Mar 3, 2011 at 19:14

7 Answers 7

1

No. I think the best way to accomplish this is to move the for loop into an else block and do the following

if (name == 'foo') {
    exampleFunction('some_arg');
}
else if (name == 'bar') {
    exampleFunction('another_arg');
}
else {
  var isFound = false;
  for (i in exampleObject) {
    if (name == exampleObject.name) {
      exampleFunction(exampleObject.arg);
      isFound = true;
    }
  }
  if (!isFound) {
    exampleFunction('default')
  }
}

Note: It looks like there are other errors in this code. The for loop declares the i iteration variable but never actually uses it. Did you mean for the if check in the for loop to use i instead of name?

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

Comments

1
if (name == 'foo') {
    exampleFunction('some_arg');
}
else if (name == 'bar') {
    exampleFunction('another_arg');
}
else {
  var isFound = false;
  for (i in exampleObject) {
    if (name == exampleObject.name) {
      exampleFunction(exampleObject.arg);
      isFound = true;
      break;
    }
  }
  if (!isFound) {
    exampleFunction('default')
  }
}

Here is the correct solution. It short circuts the if statements in the loop just like else if would short circuit. This is the same solution as #1 but it correctly short circuits.

Comments

0

The following code looks wrong to me , have the for loop inside if block

for (i in exampleObject) {
    else if (name == exampleObject.name) {
        exampleFunction(exampleObject.arg);
    }

Comments

0

that is not possible. I would try an come up with a better example to show you how to do what you want, but honestly I am not sure what you want to do. The for loop is confusing me. Can you provide some more information?

2 Comments

This probably ought to be a comment.
I thought about that, but the first part of the question was can it be done, and my answer was it is not possible. The second part of his question was show a better example, that part I could not answer without some clarification and I didn't want to ask for clarification just to the second part of his question in a comment but answer the first part in an answer
0

In a word, no. You are terminating the if-statement block with the last brace before the for statement.

Comments

0

Well for one, shouldn't this:

for (i in exampleObject) {
    else if (name == exampleObject.name) {
        exampleFunction(exampleObject.arg);
    }
}

be this:

for (i in exampleObject) {
    else if (name == i.name) {
        exampleFunction(i.arg);
    }
}

Though i don't know much (if anything) about js, this is just a guess at something that isn't even the problem you're talking about.

Would you be adverse to doing it like this:

bit = 0;
if (name == 'foo') {
    exampleFunction('some_arg');
}
else if (name == 'bar') {
    exampleFunction('another_arg');
}
else {
    bit = 1;
}

bit2 = 0;
while(bit == 1){
    for (i in exampleObject) {
        if (name == i.name) {
            exampleFunction(i.arg);
            bit = 0
            bit2 = 1;
        }
    }
}

if(bit2 = 0){
    exampleFunction('default');
}

?

1 Comment

Javascript has break. No need to set a flag to stop the inner loop.
0

Something like this may help?

found = false

if (name == 'foo') {
    found = true 
    exampleFunction('some_arg');
}
else if (name == 'bar') {
    found = true
    exampleFunction('another_arg');
}
else {
  for (i in exampleObject) {
    if (name == i.name) {
        exampleFunction(i.arg);
        found = true
        break;
    }
  }
}

if !found:
   exampleFunction('default')

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.