Everyone Can Code!

JavaScript Kata #21: Jaden Casing Strings

By YayoDrayber | Learn To Code With Me | 29 Aug 2022


If you missed the introductory post, it's here. For a list of previously solved katas, please refer to the bottom of this page.

If this is your first time seeing my post, please note - these katas are probably randomly assigned per user so please don't go into Codewars thinking we will have exactly the same user experience, I am just posting these in the order that I got them.

Kata #21

DESCRIPTION:

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"

Link to Jaden's former Twitter account @officialjaden via archive.org

Starting code:

String.prototype.toJadenCase = function () {
  //...
};

My attempt that worked:

String.prototype.toJadenCase = function(tweet) {
  let tweetToArr = this.split(' ')
  let result = ''
  let pos = 1
//   if(tweetToArr.length % 2 === 0){
    for (w = 0; w < tweetToArr.length && pos < tweetToArr.length; w++){
      tweetToArr.splice(pos,0," ")
      pos += 2
    }
//   }      
  
  for (i = 0; i < tweetToArr.length; i++){
    result += tweetToArr[i][0].toUpperCase()
    for(ii = 1; ii < tweetToArr[i].length; ii++){
      result += tweetToArr[i][ii].toLowerCase()
      }
  }
  return result
};

What a mind-melting problem. Absolutely got Jaden'd. First time to encounter adding methods to the built-in String prototype which so many resources are saying a "no-no" which took time because I had to figure out that I needed to use the 'this' keyword.

D'OH!

Without a doubt a "D'OH!" moment because I knew I needed the .split() and the .map() methods but didn't know what and how to follow through. Good thing rank #1 in 'Best Practice' shows us how:

String.prototype.toJadenCase = function () { 
  return this.split(" ").map(function(word){
    return word.charAt(0).toUpperCase() + word.slice(1);
  }).join(" ");
}

Rank #2 with what I think is RegEx, which is way far ahead for me at this point:

String.prototype.toJadenCase = function () {
  return this.replace(/(^|\s)[a-z]/g, function(x){ return x.toUpperCase(); });
};

Rank #3 with what looks like a variation of rank  #1:

String.prototype.toJadenCase = function() {
  return this.split(' ').map(item => item[0].toUpperCase() + item.slice(1)).join(' ')
};

Rank #4:

String.prototype.toJadenCase = function () {
  function capitalizeFirstLetter(string) {
      return string.charAt(0).toUpperCase() + string.slice(1);
  } 
  return this.split(' ').map(capitalizeFirstLetter).join(' ');
};

Rank #5:

String.prototype.toJadenCase = function () {
  return this.split(' ')
    .map(word => word.charAt(0).toUpperCase() + word.slice(1))
    .join(' ');
};

Rank #6:

String.prototype.toJadenCase = function () {
  var words = this.split(' ');
  for(var i = 0, wordsLen = words.length; i < wordsLen; i++) {
    words[i] = words[i][0].toUpperCase() + words[i].slice(1);
  }
  return words.join(' ');
};

Rank #7:

String.prototype.toJadenCase = function () {
  var x = '';
  x += this[0].toUpperCase();
  for (var i = 1; i < this.length; i++) {
    if (this.charAt(i) == " ") {
      x += (this[i] + this[i + 1].toUpperCase());
      i++; // Increment i to stop the function concatenating the first letter of a new word twice
    } else {
      x += this[i];
    }
  }
  return x;
};

Showing up to rank #7 because it's the longest code in the top ten approaches and yet it's loads better than mine. Goes to show how stupid and probably buggy my approach was. 🤣 Oh well. ¯\_(ツ)_/¯ Still a beginner, still got loads to learn.

That's it for Kata #21, stay tuned for more katas to be solved!

Link to Kata #1: Square(n) Sum

Link to Kata #2: Convert a Number to a String

Link to Kata #3: DNA to RNA Conversion

Link to Kata #4: Remove First and Last Character

Link to Kata #5: MakeUpperCase

Link to Kata #6: Total amount of points

Link to Kata #7: A Needle in the Haystack

Link to Kata #8: Sum of positive

Link to Kata #9: Basic Mathematical Operations

Link to Kata #10: Beginner - Reduce but Grow

Link to Kata #11: Square Every Digit

Link to Kata #12: Friend or Foe?

Link to Kata #13: Grasshopper - Summation

Link to Kata #14: Get the Middle Character

Link to Kata #15: Descending Order

Link to Kata #16: String ends with?

Link to Kata #17: Sum of two lowest positive integers

Link to Kata #18: Sum of odd numbers

Link to Kata #19: Find the next perfect square!

Link to Kata #20: Reversed Strings

Resources

  1. https://tenor.com/view/homer-simpson-les-simpson-matt-groening-ouff-non-gif-12807779
  2. https://www.publish0x.com/learn-to-code-with-me/obligatory-introductory-post-xddgyxl
  3. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-1-squaren-sum-xvmywpl
  4. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-2-convert-a-number-to-a-string-xmymkwm
  5. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-3-dna-to-rna-conversion-xgjlpox
  6. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-4-remove-first-and-last-character-xyeyykj
  7. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-5-makeuppercase-xzgnnqd
  8. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-6-total-amount-of-points-xeellvx
  9. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-7-a-needle-in-the-haystack-xlqzzgp
  10. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-8-sum-of-positive-xxzyyrl
  11. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-9-basic-mathematical-operations-xlqzzer
  12. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-10-beginner-reduce-but-grow-xxzyylv
  13. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-11-square-every-digit-xnnxxyr
  14. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-12-friend-or-foe-xddggmm
  15. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-13-grasshopper-summation-xnnxxwr
  16. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-14-get-the-middle-character-xrgnney
  17. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-15-descending-order-xmymmwn
  18. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-16-string-ends-with-xeelnpk
  19. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-17-sum-of-two-lowest-positive-integers-xjroxpn
  20. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-18-sum-of-odd-numbers-xeelnwk
  21. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-19-find-the-next-perfect-square-xozowvo
  22. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-20-reversed-strings-xvmyqyn

How do you rate this article?

1


YayoDrayber
YayoDrayber

A Yayo, A Drayber, among others.


Learn To Code With Me
Learn To Code With Me

Just started with Codewars and will be posting the solutions I came up with here and the apparent best practices.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.