Everyone Can Code!

JavaScript Kata #41: Sentence Smash

By YayoDrayber | Learn To Code With Me | 30 Sep 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 #41

DESCRIPTION:

Write a function that takes an array of words and smashes them together into a sentence and returns the sentence. You can ignore any need to sanitize words or add punctuation, but you should add spaces between each word. Be careful, there shouldn't be a space at the beginning or the end of the sentence!

Example

['hello', 'world', 'this', 'is', 'great']  =>  'hello world this is great'

Starting code:

function smash (words) {
   return ""
};

My attempt that worked:

function smash (words) {
  if (words.length===0){
    return ""
  } else {
    return words.join(" ")
  }
};

Then shortened it to:

const smash = words => words.length===0 ? "" : words.join(" ")

Only to see rank #1 in 'Best Practice' show this:

smash = function (words) {
  return words.join(" ");
};

I got got again. Apparently it says on the MDN that the separator won't be applied if there is only one element in the array, which made my code longer than it needs to be. Rank #2 shows what could've been my code if I knew that tidbit:

const smash = words => words.join(' ');

Rank #3 got fancy with the "use strict":

function smash (words) {
    "use strict";
    return words.join(' ');
};

Rank #4 is also a one-liner like rank #2 but used let:

let smash = words => words.join(" ");

Rank #5 shows how it can be solved without using the .join() method:

function smash (words) {
    "use strict";
    var smashed = '';
    for(var i = 0; i<words.length; i++) {
      smashed += words[i];
      if(i!=words.length-1) {
        smashed += ' ';
      }
    }
    return smashed;
};

And rank #6 used the .trim() string method, but is probably redundant based on .join() method's logic:

function smash (words) {
    "use strict";
    return words.join(" ").trim();    
};

That's it for Kata #41, 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

Link to Kata #21: Jaden Casing Strings

Link to Kata #22: Even or Odd

Link to Kata #23: Returning Strings

Link to Kata #24: Opposite number

Link to Kata #25: Are You Playing Banjo?

Link to Kata #26: Beginner Series #1 School Paperwork

Link to Kata #27: Remove String Spaces

Link to Kata #28: Invert values

Link to Kata #29: Count by X

Link to Kata #30: Beginner Series #2 Clock

Link to Kata #31: Is this a triangle?

Link to Kata #32: Sum of the first nth term of Series

Link to Kata #33: Find the smallest integer in the array

Link to Kata #34: Simple multiplication

Link to Kata #35: How good are you really?

Link to Kata #36: Who likes it?

Link to Kata #37: Convert a String to a Number!

Link to Kata #38: Highest and Lowest

Link to Kata #39: Equal Sides Of An Array

Link to Kata #40: Factorial Factory

Resources

  1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
  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
  23. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-21-jaden-casing-strings-xwywdwd
  24. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-22-even-or-odd-xwywdvd
  25. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-23-returning-strings-xeelxvw
  26. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-24-opposite-number-xxzyerp
  27. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-25-are-you-playing-banjo-xjrowyk
  28. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-26-beginner-series-1-school-paperwork-xxzyelp
  29. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-27-remove-string-spaces-xjrowqk
  30. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-28-invert-values-xnnxgwx
  31. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-29-count-by-x-xyeyzjz
  32. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-30-beginner-series-2-clock-xddgwmj
  33. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-31-is-this-a-triangle-xjrowxk
  34. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-32-sum-of-the-first-nth-term-of-series-xzgnjex
  35. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-33-find-the-smallest-integer-in-the-array-xpzpkkx
  36. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-34-simple-multiplication-xvmyqqd
  37. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-35-how-good-are-you-really-xjrowwz
  38. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-36-who-likes-it-xmymelp
  39. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-37-convert-a-string-to-a-number-xpzpkop
  40. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-38-highest-and-lowest-xyeyrkv
  41. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-39-equal-sides-of-an-array-xgjlmpv
  42. https://www.publish0x.com/learn-to-code-with-me/javascript-kata-40-factorial-factory-xpzzdeg

How do you rate this article?

2


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.