@Quintis

Кто может помочь разобраться с функцией?

function myReplace(str, before, after) {
  let newArr1 = []
  let arr = str.split(" ");
  console.log(arr);
  let beforeArr = before.split(" ");
console.log(beforeArr);
let register = beforeArr.join("").match(/[A-Z]/);
newArr1.push(register)
console.log(newArr1.length);
let newAfter;
if (newArr1.length > 0){
 newAfter = after.charAt(0).toUpperCase() + after.slice(1);
console.log(newAfter);
  return newAfter;
}
console.log(newAfter);
for (let i = 0; i < arr.length; i++) { 
if (arr[i]== beforeArr){
 let num =  arr.indexOf(arr[i]); 
delete arr[i];
return arr.splice(num , 1 ,after );
}                       

}
 console.log(arr.join(" "));


  // return arr.join(" ");
}

myReplace("His name is Tom", "Tom", "john");


Не могу понять почему функция не проходит тесты:
```
myReplace("Let us go to the store", "store", "mall") should return "Let us go to the mall".
myReplace("He is Sleeping on the couch", "Sleeping", "sitting") should return "He is Sitting on the couch".
myReplace("This has a spellngi error", "spellngi", "spelling") should return "This has a spelling error".
myReplace("His name is Tom", "Tom", "john") should return "His name is John".
myReplace("Let us get back to more Coding", "Coding", "algorithms") should return "Let us get back to more Algorithms".
```
link : https://learn.freecodecamp.org/javascript-algorith...

Спасибо за помощь
  • Вопрос задан
  • 125 просмотров
Решения вопроса 1
0xD34F
@0xD34F Куратор тега JavaScript
function myReplace(str, before, after) {
  if (before[0].toUpperCase() === before[0]) {
    after = after[0].toUpperCase() + after.slice(1);
  }

  return str.replace(before, after);
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
SummerWeb Ярославль
от 120 000 до 180 000 ₽
КРАФТТЕК Санкт-Петербург
от 60 000 до 80 000 ₽
Brightdata Тель-Авив
от 5 500 до 6 500 $