Tell us what’s happening:
I am trying to solve the challenge: Basic Algorithm Scripting: Title Case a Sentence. I am not using the function syntax for now, i am trying to solve the Problem without the function call but by simple String replace. But what I am getting is the ’ Final String: I’m a little tea Pot ’ i.e. only the first character of last string gets replaced.
Can anybody suggest anything?
Your code so far
var str="I'm a little tea pot";
var newStr="";
// read each character in a string
for (var i = 0; i< str.length; i++) {
// console.log(str[i]);
if(str[i] === " ") {
// read the first character after space
// console.log("first character after space:" + str[i+1])
// make the first character after space capital
// console.log("Capital the first character after space: " + str[i+1].toUpperCase())
// replace the first character after space with the Capital of the same.
// console.log("Replace character: " + str.replace(str[i+1], str[i+1].toUpperCase()))
// console.log((str[i]=str[i+1]).toUpperCase())
// store the new string in another variable.
// str += str.replace(str[i+1], str[i+1].toUpperCase());
// replace each character after space with Captial letter(manually)
var newStr = str.replace(str[i+1], str[i+1].toUpperCase())
}
}
// print the String
console.log("Final String: " + newStr);
EXPECTED OUTPUT: Final String: I’m A Little Tea Pot
ACTUAL OUTPUT: Final String: I’m a little tea Pot(pot becomes Pot)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
.
Challenge: Title Case a Sentence
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence