# Basic Algorithm Scripting - Title Case a Sentence

Hi, I can’t understand why the code doesn’t work.

``````let a = "";
let b = [];
let c = "";
let d = "";
let e = "";
let f = "";

function titleCase(str) {
a = str.toLowerCase();
b = a.split(" ");
for(let i = 0; i < b.length; i++){
c = b[i].toString();
d = c.replace(/^\w/g, c.toUpperCase());
e = e + " " + d;
f = e.slice(1);

}

return f;

}

console.log(titleCase("I'm a little tea pot"));

``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0`

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Sorry I’ve just understood the problem.

Ok, I see the problem but I can’t solve it.

`````` let a = "";
let b = [];
let c = "";
let d = "";
let e = "";
let f = "";

function titleCase(str) {
//Set f to ""
f = f.replace(/[\w+\s+'']/gi, "");
console.log(f);
a = str.toLowerCase();
b = a.split(" ");
for(let i = 0; i < b.length; i++){
c = b[i].toString();
d = c.replace(/^\w/g, c.toUpperCase());
e = e + " " + d;
f = e.slice(1);

}

return f;

}

console.log(titleCase("I'm a little tea pot"));
``````

Your code runs fine, for the three cases:

``````titleCase("I'm a little tea pot")
titleCase("HERE IS MY HANDLE HERE IS MY SPOUT")
titleCase("sHoRt AnD sToUt")
``````

And, you can declare the variables as you use them so that the number of lines of code is less - for example:
`let a = str.toLowerCase();`

Your code contains global variables that are changed each time the function is run. This means that after each function call completes, subsequent function calls start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

``````var myGlobal = ;
function returnGlobal(arg) {
myGlobal.push(arg);
return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
var myLocal = ;
myLocal.push(arg);
return myLocal;
} // reliable - always returns an array of length 2
``````