Search and Replace - Intermediate Algorithm Scripting - Free Code Camp

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

The way you explained in regards to Code refactoring is so useful : )

AJ-fkvi
Автор

Thank you so much Mr. Ian for going the extra mile as you always do with your lessons. I have a simple solution and here it is down below.
function myReplace(str, before, after) {
if (before[0] === before[0].toUpperCase()) {
after = after[0].toUpperCase() + after.substring(1)
} else {
after = after[0].toLowerCase() + after.substring(1)
}
return str.replace(before, after);
}
console.log(myReplace("This has a spellngi error", "spellngi", “spelling"));

zken
Автор

He's the longest videos for FCC Algorithms but he slowly explains everything

saltech
Автор

Here is my solution:
function myReplace(str, before, after) {
if(/^[A-Z]/.test(before) && /^[a-z]/.test(after)){
after = after.slice(0, 1).toUpperCase() + after.slice(1);
}else if(/^[a-z]/.test(before) && /^[A-Z]/.test(after)){
after = after.slice(0, 1).toLowerCase() + after.slice(1);;
}
return str.replace(before, after);
}

Again, thanks a lot for the videos. they really help.

ngoziechegini
Автор

i can't see where the replacement is talking place (we are returning individualWord ??)??

ahmedabdou
Автор

I have come up with this... is there a way to refactor it more ? In the caseTest function where there are almost identical lines written.

function myReplace(str, before, after) {

let newStr = str.split(" ")


let caseTest = function(firstLetter){
if(firstLetter === firstLetter.toUpperCase()){
after = after[0].toUpperCase() + after.slice(1)
} else if(firstLetter === firstLetter.toLowerCase()){
after = after[0].toLowerCase() + after.slice(1)
}
}

return newStr.map((arrItem) => {
if(arrItem === before){
caseTest(arrItem[0])
return after
}
return arrItem
}).join(" ")

}

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

JD-hqkn
Автор

Hey UP - hows you??

So now there is a new requirement that I think is not in your video and it requires you to:

myReplace("I think we should look up there", "up", "Down") should return "I think we should look down there".
 
So here is my solution - not as elequent as the end solution in the video but it passes all the tests :)




function myReplace(str, before, after) {

const strAll = str.split(" ");
console.log(strAll)
for(let i = 0; i < strAll.length; i++){
if(strAll[i] === before){
if(strAll[i][0] === strAll[i][0].toUpperCase()){
after = after[0].toUpperCase() + after.slice(1);
} else {
after = after[0].toLowerCase() + after.slice(1);
};

strAll[i] = after;
}
}

return strAll.join(" ");
}

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

myReplace("I think we should look up there");

Cheers :)

freecodecampdeveloper
Автор

quick question... why you iterate by one each time and not i++ times if you need to iterate over all the string?
hope you can solve this doubt!

thanks :)

valeriasalas
Автор

Nice, helps a lot for understanding how to do this task. For now, an additional test needs to add:
myReplace("I think we should look up there", "up", "Down") should return the string I think we should look down there.

My solutions by you explaining:

function myReplace(str, before, after) {
let strCollection = str.split(" ")

function {
return wordUp[0] === wordUp[0].toUpperCase()
}
function {
return wordLw[0] === wordLw[0].toLowerCase()
}

for (let i = 0; i < strCollection.length; i += 1) {
if (strCollection[i] === before) {
if {
after = after[0].toUpperCase() + after.slice(1);
};
if {
after = after[0].toLowerCase() + after.slice(1);
};
strCollection[i] = after;
};
};
return strCollection.join(" ");
}

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

console.log(result)

DantesSagan
Автор

Thx!
My code:

function myReplace(str, before, after) {
if(/[A-Z]/.test(before[0])) {
after = after[0].toUpperCase() + after.substring(1);
} else {
after = after[0].toLowerCase() + after.substring(1);
}
return str.replace(before, after);
}

gootibi
Автор

I think this is easier:
function myReplace(str, before, after) {
after = after.split('');
if(before[0] == before[0].toUpperCase())
after[0] = after[0].toUpperCase();
else
after[0] = after[0].toLowerCase();
after = after.join('');
return str.replace(before, after);
}
myReplace("He is Sleeping on the couch", "Sleeping", "sitting");

silviugeorge
Автор

now the challenge has had a change, when "before" is lowercase "after" also has to be lowercase. For which you can add this part to the code.
function myReplace(str, before, after) {
let stringCollection=str.split(" ");
for(let i



}else{

}
stringCollection[i]=after;
};
}
return stringCollection.join(" ");
}

let result= myReplace("He is Sleeping on the couch", "Sleeping", "sitting");
console.log(result);

pedrobarrientos
Автор

Here is my solution,
function myReplace(str, before, after) {
let index = str.indexOf(before);

after= after.charAt(0).toUpperCase() + after.slice(1)
}
else{
after = after.charAt(0).toLowerCase() +after.slice(1)

}

let result = str.replace(before, after)
return result
}

console.log(myReplace("I think we should look up there", "up", "Down"))

kiransundal
Автор

function myReplace(str, before, after) {
const beforeArr = before.split("")
const afterArr = after.split("")
if(before.charAt(0) ===
afterArr[0] = afterArr[0].toLowerCase()
} else{
afterArr[0] = afterArr[0].toUpperCase()
}

const beforeStr = beforeArr.join("")
const afterStr = afterArr.join("")
return str.replace(beforeStr, afterStr)
}

halochant