Ok. This way I chose to try and solve this challenge is not using the suggested methods. I am trying to challenge myself because I think it is possible to complete this using only my primitive methods. I am so close to defeating it, but one thing remains amiss. I am trying to change the value of my array[x] into (" " + array[x]) to put a space before each capital letter in what would be a string to make it a sentence. The problem im finding (and this is weird and interesting) is that instead of adding a preceding space and moving to the next letter that fires the loop, it does that PLUS adds another space to the first letter that met the condition, and keeps going so for instance if you have a sentence with 5 words and you skip the first letter that leaves four capital letters that will fire the loop. The fist one has 4 spaces, next one has three, etc until the last one has only one. My question is this: How can I make this stop adding spaces to the previous instances of array[x] ??? Knowing the answer to this will help me learn exponentially. Thanks ahead of time guys!
Heres my code:
function titleCase(str) {
var low = str.toLowerCase();
var splits = low.split(" ");
var array = [];
for (var i = 0; i < splits.length; i++) {
array.push(splits[i][0].toUpperCase());
for (var h = 1; h < splits[i].length; h++) {
array.push(splits[i][h]);
}
for (var x = 1; x < array.length; x++) {
if (array[x] === array[x].toUpperCase() && array[x] !== "'") {
array[x] = (" " + array[x] );
/*This is where I added a break statement to have the "x" for loop run only once. What I found out is that, after a single iteration, as it finds the first capital letter I ask it to ("A" after skipping the first letter), it adds four spaces preceding the "A". Without the break added, it will add one less space to each successive capital letter.
*/
}
}
var joined = array.join("");
}
return joined;
}
titleCase("I'm a little tea pot");
Your code so far
function titleCase(str) {
var low = str.toLowerCase();
var splits = low.split(" ");
var array = [];
for (var i = 0; i < splits.length; i++) {
array.push(splits[i][0].toUpperCase());
for (var h = 1; h < splits[i].length; h++) {
array.push(splits[i][h]);
}
for (var x = 1; x < array.length; x++) {
if (array[x] === array[x].toUpperCase() && array[x] !== "'") {
array[x] = ("0" + array[x] );
break;
}
}
var joined = array.join("");
}
return joined;
}
titleCase("I'm a little tea pot");
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:54.0) Gecko/20100101 Firefox/54.0.1 Waterfox/54.0.1
.
Link to the challenge: