Search and Replace::

Search and Replace::
0

#1

Tell us what’s happening:
str.indexOf(re));//why it is not giving other number than -1
i changed str also to before, word, also added some words with capitals but gives just -1;

Your code so far

  
  //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;
  //console.log(before.indexOf(re));
  
    //document.write(str.indexOf(re));//why it is not giving other number than -1; it should check Capital letter
  if(word.indexOf(re) === -1) {
    
    str2.splice(a, 0,after);
    return str2.join(" ");
  }else {
	var capitalLetter = after[0];
	var newWord = capitalLetter.toUpperCase();
	return newWord;
  }
//document.write(str2);
}

//myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
myReplace("He is Sleeping on the couch", "Sleeping", "sitting");

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

Use this to find any upper alphabet character. if assuming all the characters is ASCII.

  var indexOfUpperChar = -1;
  for(var i =0; i<str.length; i++){
      if(str[i].charCodeAt(i) < 'A'.charCodeAt(0)
            && str[i].charCodeAt(i) > 'Z'.charCodeAt(0)){
            indexOfUpperChar = i;
        }
  }