0

I have a problem with using a function within a loop, the function contains a switch-statement and it breaks up the code (literally) because the switch-statement contains breaks.

function presentation(input, fixedid)
{
    switch(fixedid)
    {
        case 1:
            output = namesplice(input);
            break;
        case 6:
            output = str_remain(input, '0123456789');
            break;
        case 8:
            output = str_replace(' ', '', strtoupper(input));
            break;
        case 11:
            output = str_remain(input, '0123456789');
            break;
        default:
            output = input;
            break;
    }

    return output;
}

for (i=1;i<=cellCount;i++)
{
    label = $('#cell0_'+ i).html();

    for (j=1;j<=count(targetcolumns);j++)
    {
        if (jQuery.inArray(strtolower(label), sub[targetcolumns[j] + '_label']) >= 0)
        {
            result = $('#content'+ i).html();
            result = presentation(result, j);
            $('#result'+ j).append(result);
        }
    }
}

It goes wrong on this piece of code: result = presentation(result, j);

What the rest of the code does isn't really important, the only thing you should know that it is within two loops. The function gets called and then breaks out of the first loop, starting it over again and thus creating a unending loop.

How can I use the function without breaking up the rest of my code?

4
  • What does It goes wrong wrong mean? What does breaks out of the first loop mean here? What is happening? What should happen? Are count, strtolower and all the other functions actually defined? Looks a bit like a mixture of PHP and JavaScript. Commented Mar 15, 2012 at 13:12
  • When using the function, the result appended to the div with id result1 is an unending chain of the same value. All other functions are defined and work. I use PHPjs, thus the similarity to PHP. When I call the function presentation directly, so not within a loop it works fine. Commented Mar 15, 2012 at 13:25
  • An "unending" chain implies that you have an infinite loop, but then the code would just hang and never show the values. What happens when you run the code, and how many values are there? Commented Mar 15, 2012 at 13:49
  • While making an example on jsfiddle I found the problem to be the str_replace function, in certain combinations it seems to break loops. Commented Mar 15, 2012 at 16:17

1 Answer 1

0

No, it doesn't.

The break statements in the function only breaks out of the switch, they doesn't affect the program flow of the code that calls the function.

Any problem that you might have with the loops (which is not clear from the question), doesn't come from using break inside the function.

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

1 Comment

The problem came from the str_replace function, too eager to work with something new!

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.