function titleCase(str) {
var lowerCase = str.toLowerCase();
var newstr = lowerCase.split(’ ');
toUpperCase = newstr.map(function(x) {
return x.charAt(0).toUpperCase() + x.slice(1).toLowerCase();
});
var result = toUpperCase.join(" ");
return result;
}
titleCase(“I’m a LITtle tea pot”);
Hello here is that my fonction
function titleCase(str) {
return str.toLowerCase().split(’ ‘).map( function (word) {
return word[0].toUpperCase() + word.substr(1);
}).join(’ ');
}
but i wrote this code if i do a console.log() it print the right string but if i copied it in fcc editor it doesn’t work i don’t know why (THIS IS THE FIRST FUNCTION I WROTE)
function titleCase(str) {
str = str.toLowerCase();
var arr = str.split(" “);var st = “”;
for(var i = 0; i < arr.length; i++){
st += arr[i].replace(arr[i][0],arr[i][0].toUpperCase())+” ";
}
return str;
}
I started challenge from the top and here is my new script
function titleCase(str) {
var arr = [];
arr = str.split(' ');
var retour = arr.map(function(element){
var tmp = [];
tmp.push(element.charAt(0).toUpperCase());
for(var i = 1; i < element.length; i++){
tmp.push(element[i].toLowerCase());
}
return tmp.join('');
});
return retour.join(' ');
}
I don’t know why but I read the explanation and I can’t understand the solutions added. Although I solved the challenge, I think there is something wrong or advanced. here is my Solution:
function titleCase(str) {
var splitstr = str.toLowerCase().split(" “);
var result = [];
for (var x = 0 ; x < splitstr.length ; x++ ){
var word = splitstr[x].split(”");
var letter = word.shift();
var up = letter.toUpperCase();
word.unshift(up);
result.push(word.join(""));
}
return result.join(" ");
}
titleCase(“I’m and little tea pot”);
function titleCase(str) {
str = str.toLowerCase().split(" "); //Make str all lowercase and convert to array
for (var i = 0; i < str.length; i++){ //Loop through array elements
str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1);
}
return str.join(" ");
}
str[i] = str[i].charAt(0) replaces the entire array element with it’s own first letter. Followed by .toUpperCase to capitalize the word.
Because the new value assigned to that position is only the first letter of the word, we + str[i].slice(1) to attach the rest of the word. .slice(x) works by returning a substring from position x until the last position.
function titleCase(str) {
var lCase = str.toLowerCase();
var array = lCase.split(" ");
var i = 0;
var i2 = 0;
var word;
var letter;
var result;
while (i < array.length) {
word = array[i];
letter = word.charAt(0).toUpperCase();
i2 = 0;
while (i2 <= word.length) {
if (i2 == 0) {
if (i == 0) {
result = letter;
}
else {
result = result + letter;
}
}
else {
result = result + word.charAt(i2);
}
i2++;
}
if (i == array.length -1) {
result = result;
}
else {
result = result + " ";
}
i++;
}
return result;
}
titleCase("I'm a little tea pot");
function titleCase(str) {
var newArr = [];
var newStr = "";
var lowerCaseArr = str.toLowerCase().split(" ");
for(i=0;i<lowerCaseArr.length;i++){
newArr.push(lowerCaseArr[i].replace(lowerCaseArr[i][0], lowerCaseArr[i][0].toUpperCase()));
}
newStr = newArr.join(" ");
return newStr;
}
titleCase("I'm a little tea pot");
The solutions are WAAAAY different. Didn’t even learn about the prototypes yet, so I’m a bit nervous that we’re expected to know stuff we haven’t yet covered. But at least I got it to work on my own.
Hi, I didn’t understand the solution above, so i made my one, And it’s work fine for me:
function titleCase(str) {
var newTitle = str.toLowerCase().split(' ');
var updatedTitle = [];
newTitle.forEach(function(item){
updatedTitle.push(item.replace(item.charAt(0), item.charAt(0).toUpperCase()));
});
return updatedTitle.join(' ');
}
titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");
I wrote the following code, and it works in the console here at code camp (and at the javascript sandbox), but I’m still getting X’s. Here’s my code:
function titleCase(str) {
str = str.toLowerCase();
str = str.split(" ");
var sent = "";
for(var i = 0; i < str.length; i++){
var first = str[i][0].toUpperCase();
var second = str[i].substring(1, str[i].length);
var word = first + second;
sent += word + " ";
}
return sent;
}
titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");
Steps:
1 and 2. Turn str into lower case and split into individual words at each space.
3. Create empty string variable to place my new sentence in.
4. Loop through the original sentence, grab first letter of each word and turn to upper case,
deduct first letter of each word, combine upper case letter with deducted version of each word.
5. Put each new word into the sent var (plus a space).
6. Return new sentence
Not sure why code camp registers the right answer, but doesn’t accept it as correct.
I wanted to create a solution that didn’t use toUpperCase() or toLowerCase() methods I ended up using ASCII characters instead. I have attached the image of my solution below
This is my solution. Not the best, but I tried to do it without looking at the solution first.
By the way, I was kind of surprised toUpperCase() method worked on chars, I thought it was exclusively for strings…?
function titleCase(str) {
var arr = str.toLowerCase().split(' ');
var newArr = [];
for(i = 0; i < arr.length; i++) {
newArr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
}
return newArr.join(' ');
}
titleCase("I'm a little tea pot");
question: Why are you assigning a value to arrayStr[i]?
E.g. arrayStr[i] = arrayStr[i].replace(/[a-z]/, arrayStr[i].substring(0,1).toUpperCase());
I tried using a different variable name, but it wouldn’t work any other variable.
E.g. newStr = splitStr[i].replace(splitStr[i].charAt(0),
It’s a global variable; I had already defined it.
I’ve had such hard time solving this exercise. I finally did it after looking at this page, but it feels I have copied the answer even with minor tweaks.
I’ve copied 2 of my codes (I tried a few) before I reviewed this site. They DO NOT work, but not sure why. What am I NOT seeing? Please Help!!!
ATTEMPT #1
function titleCase(str) {
var splitStr = str.toLowerCase().split(" “);
var capitalizeFirstWord = (function(){
splitStr.replace(splitStr.chartAt(0), splitStr.chartAt(0).toUpperCase());
});
return capitalizeFirstWord.join(” ");
}
titleCase(“i’m a little tea pot”);
ATTEMPT #2
function titleCase(str) {
var splitStr = str.toLowerCase().split(" “);
var capitalizeFirstWord = function(val){
return val.replace(val.chartAt(0), val.chartAt(0).toUpperCase());
};
return capitalizeFirstWord.join(” ");
}
titleCase(“I’m a little tea pot”);