Last active
August 8, 2023 08:21
-
-
Save geekelo/95cde9e07afdb79159612519af5d5e7c to your computer and use it in GitHub Desktop.
Jaden Casing Strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QUESTION: | |
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. For simplicity, you’ll have to capitalize each word, check out how contractions are expected to be in the example below. | |
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” | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SOLUTION ONE (with map method) | |
String.prototype.toJadenCase = function () { | |
// 'this' refers to the string instance | |
const words = this.split(' '); | |
const capitalizedWords = words.map((word) => { | |
// Capitalize the first letter of each word | |
return word.charAt(0).toUpperCase() + word.slice(1); | |
}); | |
// Join the words back together to form the Jaden Case string | |
return capitalizedWords.join(' '); | |
}; | |
const sentence = "How can mirrors be real if our eyes aren't real"; | |
const jadenCaseSentence = sentence.toJadenCase(); | |
console.log(jadenCaseSentence); // Output: "How Can Mirrors Be Real If Our Eyes Aren't Real" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SOLUTION TWO (with for loop) | |
String.prototype.toJadenCase = function () { | |
//... | |
let str = this.split(''); | |
for(let i=0; i < str.length; i++){ | |
if(str[i] === ' '){ | |
value = str[i + 1].toUpperCase(); | |
str[i + 1] = value; | |
} | |
} | |
str = str.join('') | |
console.log(str); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Test = require('@codewars/test-compat'); | |
describe("Tests", () => { | |
it("test", () => { | |
var str = "How can mirrors be real if our eyes aren't real"; | |
Test.assertEquals(str.toJadenCase(), "How Can Mirrors Be Real If Our Eyes Aren't Real"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment