For Loop Question

For Loop Question
0

#1

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!


#2

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

More info here.
https://www.w3schools.com/js/js_break.asp


#3

thanks that was helpful


#4

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.


#5

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