Skip to content

Instantly share code, notes, and snippets.

@lindsaycarbonell
Created February 14, 2019 21:28
Show Gist options
  • Save lindsaycarbonell/188e1b8818b7f8f1b61bde23576ca22d to your computer and use it in GitHub Desktop.
Save lindsaycarbonell/188e1b8818b7f8f1b61bde23576ca22d to your computer and use it in GitHub Desktop.
kata: Jaden Casing Strings

Jaden Smith, the son of Will Smith, is the star of films such as The Karate Kid (2010) and After Earth (2013). Jaden is also known for some of his philosophy that he delivers via Twitter. When writing on Twitter, he is known for almost always capitalizing every word.

Your task is to convert strings to how they would be written by Jaden Smith. The strings are actual quotes from Jaden Smith, but they are not capitalized in the same way he originally typed them.

Example:

Not Jaden-Cased: "How can mirrors be real if our eyes aren't real" Jaden-Cased: "How Can Mirrors Be Real If Our Eyes Aren't Real"

Note that the Java version expects a return value of null for an empty string or null.

String.prototype.toJadenCase = function () {
var newStr = "";
this.split(" ").forEach(function(s) {
newStr = newStr + " " + s.substring(0,1).toUpperCase() + s.substring(1);
});
return newStr.substr(1);
}
//FIRST SOLUTION
String.prototype.toJadenCase = function () {
return this.split(" ").map(function(word){
return word.charAt(0).toUpperCase() + word.slice(1);
}).join(" ");
}
//SECOND SOLUTION
String.prototype.toJadenCase = function () {
return this.replace(/(^|\s)[a-z]/g, function(x){ return x.toUpperCase(); });
};
//THIRD SOLUTION
String.prototype.toJadenCase = function () {
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
return this.split(' ').map(capitalizeFirstLetter).join(' ');
};
@micksteph
Copy link

Hi, my name is Mickael and I have three weeks learning Javascript. I found this little test and I tried to resolve but I couldn't. Now I found your answer, and I want to know if can you explain to me the way that you use your code to resolve this challenge in this three different ways.

thank you to read me!

@sanjaytj
Copy link

sanjaytj commented Apr 9, 2021

The first solution is simple.
We first split the words in the given string after each " " space is detected (in the split(" ") )
And then we apply the map method which calls the function word . (map applies what happens in the function to each and every item in the array which has been split from the given string.

In the function word = Each item of the array is taken and the first letter which is index value 0 ( charAt(0) ) is converted to Upper Case and is concatenated with the rest of the letters of the word (excluding the first letter (word.slice(1))

Then using the Join method all the words are joined to make a sentence

Thus it returns the sentence with the First letter of each word Capitalized

@Ivan-hor
Copy link

Ivan-hor commented Jul 8, 2021

This is my solution...

  return this.split(' ').map(el => el[0].toUpperCase() + el.slice(1)).join(' ')
};

@GooferG
Copy link

GooferG commented May 7, 2022

I was stuck on how to make the method work, I had the idea of the solution but was telling me it couldn't run because it was undefined. Then I realized I should be using this since this was a method. Awesome work mate!

@Lebacon7
Copy link

Thanks - formatting helped me realize my issue.

@Bassam15
Copy link

I was stuck on how to make the method work, I had the idea of the solution but was telling me it couldn't run because it was undefined. Then I realized I should be using this since this was a method. Awesome work mate!

I was stuck on how to make the method work, I had the idea of the solution but was telling me it couldn't run because it was undefined. Then I realized I should be using this since this was a method. Awesome work mate!

if you need to test your answer
code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment