Hi there, here is my solution to the problem. Capitalizing the first character of each element of string array ‘str’ then ‘lowercasing’ the other characters in the remainder of each element. Join them all up afterwards…voila!
function titleCase(str) {
str = str.split(’ ');
var strlen = str.length;
var strarr = [];
var strslice = [];
for (var i = 0; i < strlen; i++)
{
strarr[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1).toLowerCase();
}
return strarr.join(" ");
}
function titleCase(str) {
var words = str.toLowerCase().split(’ ‘);
var upper = [];
for (var i = 0; i < words.length; i++) {
upper.push(words[i].charAt(0).toUpperCase() + words[i].slice(1));
}
return upper.join(’ ');
}
Can anyone help me, I have no idea why this doesn’t pass all cases.
function titleCase(str) {
var arr = str.split(’ ‘);
var newStr = ‘’;
for (var i = 0; i < arr.length; i++){
var word = arr[i].split(’’);
for (var j = 0; j < word.length; j++){
if (arr[i][j]==arr[i][0]){
newStr += arr[i][0].toUpperCase();
} else{
newStr += arr[i][j].toLowerCase();
}
}
if (i + 1 != arr.length)
newStr += ’ ';
}
return newStr;
}
titleCase(“I’m a little tea pot”);
Here is what I was able to come up with for my solution using splice.
function titleCase(str) {
var lowerCase = str.toLowerCase();
var wordArray = lowerCase.split(" ");
var newWord = [];
var newLetter = [];
var finalWord = “”;
for (i = 0; i < wordArray.length; i++){
newLetter += wordArray[i][0].toUpperCase();
newWord += wordArray[i].replace(wordArray[i][0], newLetter[i]) + ’ ';
While my code for this challenge was a little dirty, I was able to finish it. However, although the output is correct, it does not move on to the next challenge. Anyone else have this problem?
Same here! I wrote the following code which seems to give all the desired outputs, yet I only get a checkmark for the first criteria but a red X for the other three.
function titleCase(str) {
var arr = str.split(" ");
var arr2 = [];
for (i = 0; i < arr.length; i++) {
arr2 += arr[i].charAt(0).toUpperCase() + arr[i].slice(1).toLowerCase() + " ";
}
return arr2;
}
titleCase("I'm a little tea pot");
EDIT: Of course, I realized my error right after I posted. I changed the final return line to:
function titleCase(str) {
var arr1 = str.toLowerCase().split(" ");
var arr11 = [];
for (a = 0; a < arr1.length; a++) {
arr11.push(arr1[a][0].toUpperCase() + arr1[a].slice(1));
}
var arr2 = arr11.join(" ");
return arr2;
}
titleCase("I'm a little tea pot");
titleCase(Str) {
var oldArr = str.toLowerCase().split(' ');
var newArr = [];
for (var i in oldArr) {
var newWord = oldArr[i][0].toUpperCase() + oldArr[i].slice(1);
newArr.push(newWord);
}
return newArr.join(' ');
}
there’s no need for:
for (var i = 0; i < oldArr.length; i++) {
}
because:
for (var i in oldArr) {
}
will loop through the entire length of oldArr without the need of a loop counter.
Solution: The online compiler thing does not like it when I place global variables for some reason?
function titleCase(str)
{
var string=""; //<----buffer variable
str=str.toLowerCase().split(' ');
for(a=0;a<=str.length-1;a++)
{
string+=str[a][0].toUpperCase();
for(var b=1;b<str[a].length;b++)
{
string+=str[a][b];
}
if(a==str.length-1) continue;//
else string+=' '; // Do not place a space on a final word
}
return string;
}
titleCase("tHe wIZARD oF oZ");
//"The Wizard Of Oz"