OneCompiler

replace function

159

// replace method using function

function replace(first, second , p){

var world = "";
var result = [];

for (var i = 0; i < p.length; i++) {
if(p[i] ==" "){
if (world == first) {
result.push(second);
world = "";
}else{
result.push(world);
world= "";
}

}else{
  world = world +p[i];
}

}
console.log(result.join(" "))
}

var p = "function the quick brown fox jump over the lazy dog if the dog reacted was it really lazy";

replace("dog", "fox", p);