I had been spending (two) hours trying to figure this one out after learning what I can and can’t do with arrays.
This is what I did- it’s not the most efficient but it’s what made sense logically, plus i kind of mixed up es6 in there as I was wondering if that stopped my code from working.
Convert String to lowercase and store in an array.
Initialise an empty array.
for loop that runs the length of the String passed which copies elements into new array by:
checking if it’s the first element of the array and capitalises this, otherwise copies over the array
If the array element is preceded by an empty space capitalises said element (first letters of words are preceded by empty spaces)
Solution below (hidden)
Summary
function titleCase(str) {
var lowerCase=str.toLowerCase(); //convert string to lower case
var b=lowerCase.split(""); //split string into
var arr=[]; //initialised array
for(let i=0;i < b.length; i++){ //for loop for length of string
arr.push(b[i]); //maps element in b to arr
if (i == 0){ // checks first element and capitalise, replacing lowercase element
arr.pop(); //remove lowercase
arr.push(b[i].toUpperCase());
}
if(b[i-1] == " ")
{ //check to see if previous element is empty string
arr.pop();
arr.push(b[i].toUpperCase()); //remove present element and replace with capitalised
}
}
return arr.join(""); //join array so it's a string
}
titleCase("I'm a little tea pot");
Check that in your editor you have the actual character you want instead of &lt;
Bonus point if you can make it more efficient. You don’t need to separete the string in an array made of all the characters and then loop over each character, you can make a code that need a lot less steps
that’s not true, there are situations in which a loose comparison is useful. And situations in which you must use the other one. But it is situational.
That is not an empty string, that is a space. An empty string is "" (see? empty) and it is different than a space.
Why add this extra step anyway? Can’t you make the check before adding the character to the array?
Do you really need to loop over all characters? Is there an other way in which you can identify the first character of each word?
If I have to compare a number from a string to a variable, using == will need less code written and improve readability. The first YDKJS JavaScript makes a good point on the use of the equality operators
Point taken about b[-1], although wouldn’t it not do anything in this case as you can’t have that index. I guess I could add an extra condition to check that b is at least 1.
What happened is that when adding the element was that it was adding in lower case. So i removed the lower case and replaced with upper case.
What I could’ve done in hindsight is two ifs statement
first checking if the index is 0 to capitalise
then checking the first letter of each word to categorise (I used checking to see if the previous element was a " ")
if neither is the case, then copying over the element in the exact case
You could divide the string into words (str.split(' ')), in this way your loop would be much more shorter, and use replace(). Try to solve in this way too, just for practice.
I couldn’t think of another way to add the first character to each word at the time.
I don’t know what challenge you’re doing,. but if you’re looking to capitalize every first letter of each word then here’s an example.
var sentence = "teapot is my favorite";
var newWord = sentence.split(' ').map(function(word){
return word[0].toUpperCase() + word.slice(1);
}).join(' ');
and es6:
const sentence = "teapot is my favorite";
const newWord = sentence.split(' ').map((word) => word[0].toUpperCase() + word.slice(1)).join(' ');