For Loop Question

I am on the search and replace challenge and my question is can i terminate my for loop in the method that i did or is there another more acceptable way?

function myReplace(str, before, after) {
  str=str.split(" ");
  for ( var i;i++;i===str[-1]) {

    if (i== before) {
  i = after;
  return str;

myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

Thanks in advance!

You can jump out of a for loop using the break; statement.

More info here.

1 Like

thanks that was helpful

hi cisd,

I don’t believe this for loop will run as intended.

The first parameter that is passed in is undefined. Intuitively, I don’t see how it can you can increment an undefined data value.
The second parameter should be the conditional for termination.
The third parameter is when the variable declared in the first parameter is incremented.

its alright i figured it out and moved on sorry for the inconvenience but thanks though