Create a function that accepts three parameters:
- A string where you want to replace a letter.
- The letter you want to replace (the second argument).
- The letter you want to use as a replacement (the third argument).
This function should replace all occurrences of the second letter with the third letter in the given string.
Solution
// searchAndReplace.js
function searchAndReplace(text, firstLetter, secondLetter) {
const replacedString = text
.split("")
.map((item) => (item === firstLetter ? secondLetter : item))
.join("");
console.log(replacedString);
return replacedString;
}
searchAndReplace("Drastic measures made fast", "a", "o");
Result
> Drostic meosures mode fost