# Search and Replace Algorithm Question

Hi, I’m having difficulty getting a solution to this. Can someone let me know why I can’t seem to convert this array to a string with join?

``````function myReplace(str, before, after) {
let newStr = str.split(" ");
console.log(newStr = str.replace(before, after)); //Not case sensitive version
console.log("Here are the before and after: " + before + ", " + after); //Shows both words
let beforeArr = before.split(""); //Splits all letters off into an array of elements
console.log("beforeArr is " + beforeArr);
let beforeLet = beforeArr.shift("");  //Removes and returns first letter
console.log("beforeLet is " + beforeLet);
let afterArr = after.split(""); //
console.log("afterArr is " + afterArr);
let afterLet = afterArr.shift("");
console.log("afterLet is " + afterLet);

if (beforeLet.match(/[A-Z]/)) {
let upperAfterLet = afterLet.toUpperCase();
console.log("upperAfterLet is " + upperAfterLet);
afterArr.unshift(upperAfterLet);
console.log("afterArr is " + afterArr);
afterStr = afterArr.join();
console.log("afterArr is " + afterStr);
console.log(str.replace(before, afterStr).join);
return str.replace(before, afterStr).join;
} else if (beforeLet.match(/[a-z]/)) {
let lowerAfterLet = afterLet.toLowerCase();
console.log("lowerAfterLet is " + lowerAfterLet);
afterArr.unshift(lowerAfterLet);
console.log("afterArr is " + afterArr);
afterStr = afterArr.join();
console.log("afterArr is " + afterStr);
console.log(str.replace(before, afterStr).join);
return str.replace(before, afterStr).join;
} else {
console.log(str.replace(before, after).join);
return str.replace(before, after).join;
}
//return newStr = str.replace(before, after).join;
}

myReplace("He is Sleeping on the couch", "Sleeping", "sitting");
myReplace("This has a spellngi error", "spellngi", "spelling");
myReplace("Let us get back to more Coding", "Coding", "algorithms");
``````
``````CONSOLE/NODE:

PS C:\Users\EnigmaBox\Desktop\JavascriptLearning\js-projects> node testfunctions.js
He is sitting on the couch
Here are the before and after: Sleeping, sitting
beforeArr is S,l,e,e,p,i,n,g
beforeLet is S
afterArr is s,i,t,t,i,n,g
afterLet is s
upperAfterLet is S
afterArr is S,i,t,t,i,n,g
afterArr is S,i,t,t,i,n,g
undefined

This has a spelling error
Here are the before and after: spellngi, spelling
beforeArr is s,p,e,l,l,n,g,i
beforeLet is s
afterArr is s,p,e,l,l,i,n,g
afterLet is s
lowerAfterLet is s
afterArr is s,p,e,l,l,i,n,g
afterArr is s,p,e,l,l,i,n,g
undefined

Let us get back to more algorithms
Here are the before and after: Coding, algorithms
beforeArr is C,o,d,i,n,g
beforeLet is C
afterArr is a,l,g,o,r,i,t,h,m,s
afterLet is a
upperAfterLet is A
afterArr is A,l,g,o,r,i,t,h,m,s
afterArr is A,l,g,o,r,i,t,h,m,s
undefined
``````

The critical piece is this:

``````afterStr = afterArr.join();
console.log("afterArr is " + afterStr); //afterArr is S,i,t,t,i,n,g
console.log(str.replace(before, afterStr).join); //undefined
return str.replace(before, afterStr).join;
``````

You must supply a separator argument to the join method.

2 Likes

first off your afterStr variable is not declared anywhere so add `let afterStr;` to the top of the function and for your join you need to use it like this `.join('')`

1 Like

Thanks

I’ve was messing with the code using different variables, so that’s probably why. I forgot that join needs a seperator, so once that got fixed I caught the Syntax issues and fixed those. All good,

Thank you guys!