Search and Replace::Having problem::

Search and Replace::Having problem::
0

#1

Tell us what’s happening:
Writes is not str.splice is not function…i did not assign str.splice as function

Your code so far

function myReplace(str, before, after) {
  
  //str = str.replace(before, after);
  var word = str.splice(str.indexOf(before),str.indexOf(before) + before.length);
  var re = /[A-Z]/g;
  
    
  if(word.search(re) === -1) {
    
    str.replace(before, after);
  }
  
  return str;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36.

Link to the challenge:


#2

The splice method is only for arrays. I think what you’re looking for is slice.

Your function might still have some issues, though. I think I see what you’re trying to do with String.search but I see two problems:

  1. The return value of slice is the extracted part of the string, not the whole string that’s left after extraction. So var word = str.slice(...) is going to give you the word you extracted, .ie., the before argument, rather than the string minus the before arg, which I think is what you’re going for there.
  2. That regular expression /[A-Z]/g is going to match any uppercase character in the alphabet. Is that what you want? It looks to me like you want to match only the before arg, no?

#3

i did it another way, but now having trouble with toUpperCase func

function myReplace(str, before, after) {
  
  //str = str.replace(before, after);
  var str2 = str.split(" ");
  var a = str2.indexOf(before);
  var word = str2.splice(a,1);
  var re = /[A-Z]/g;
  
    
  if(word.indexOf(re) === -1) {
    
    str2.splice(a, 0,after);
    return str2.join(" ");
  }else {
	after[0].toUpperCase();
	return str2.splice(a,0,after);
}
}

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

#4

toUpperCase returns only the string (or parts of a string) that it operated on. In your example after[0].toUpperCase() doesn’t change the original after argument. Rather it returns the first character of after, capitalized. So if the argument is jumped then after[0].toUpperCase() returns J.

You’ll want to concatenate that capitalized return value with the rest of the original argument. I used String.replace() like this:

function myReplace(str, before, after) {
  var str2 = str.split(" ");
  var a = str2.indexOf(before);
  var word = str2.splice(a,1);
  var re = /[A-Z]/g;
  
  if (word[0].search(re) === -1) {
    str2.splice(a, 0, after);
    return str2.join(" ");
  }
  else {
    after = after.replace(after[0], after[0].toUpperCase());
    str2.splice(a,0,after);
    return str2.join(" ");
  }
}

A couple of other things to note there:

  • In your if condition you have word[0].indexOf(re). I couldn’t get that to do anything for me. I’m not sure regex works with indexOf. To my knowledge there are only four methods that use regex: search, replace, exec, and test. Use search. That’s what you had before it works fine. (Side note, has anyone come up with a mnemonic for those methods? It seems ripe for one, such as REST or TERS. or ERST)
  • Remember the return value of splice is the spliced out element, not the array left after the removal, so at the end of the else statement when you return str2.splice(a, 0, after) you’re returning an empty array [] because that’s what’s been spliced out. You’ll need to splice in your new word and then join and return the whole string that was added to, just like you did above in the if statement.

#5

Here is my way

function myReplace(str, before, after) {
  if(before[0] == before[0].toUpperCase()) {
    str = str.replace(before, after.slice(0, 1).toUpperCase() + after.slice(1, after.length));
  } else {
    str = str.replace(before, after);
  }
  return str;
}

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

#6