Javascript Freecodecamp Algorithm #7: Search and Replace

preview_player
Показать описание
Learn how to solve freecodecamp javascript algorithms in various ways! This series is up-to-date with all ES6 and beyond javascript notations
Рекомендации по теме
Комментарии
Автор

hello All, that my personal solution on Freecodecamp, it's low confusing but it work well :



function myReplace(str, before, after) {
var v = str.indexOf(before)
var a =str.replace(before, before[0].match(/[A-Z]/)?after[0].toUpperCase().concat(after.slice(1, after.length)):after[0].toLowerCase().concat(after.slice(1, after.length)))
return a
}

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

customxvega
Автор

could we just split it into an array, loop through it and if there anything matches with the before, replace it with the after and then break from the loop. Then just join it back to string

MrZiyak
welcome to shbcf.ru