DEV Community

Cover image for #2 Live Kata Report
Raffaele Pizzari
Raffaele Pizzari

Posted on • Updated on

#2 Live Kata Report

2 Live Kata Report

With the JS Learning community I am running we use to solve "JS Katas" together during "live coding" sessions.
I'll use this blog to share the "Live Kata" reports.

We solved together a "kata" found on "CodeWars.com".

Key takeaway points:

• Learn how String.prototype.slice() works
• Learn how Array.prototype.join() works
• Learn how Array.prototype.reduce() works
• Learn how "Destructuring assignment" works

Docs:

Array.prototype.reduce()
String.prototype.slice()
Array.prototype.join()
Destructuring assignment
• A website full of katas

The Kata is the following:

Requirements:

Move the first letter of each word to the end of it, then add "ay" to the end of the word.

Example

'Pig latin is cool' => igPay atinlay siay oolcay
'Hello world' => elloHay orldway

Our solutions

Solution 1

function doKata(str) {
  const arr = str.split(' ');
  const sentence = arr.map((e) => {
    const rest = e.slice(1);
    const first = e[0]
    const suffix = 'ay';
    const word = `${rest}${first}${suffix}`;
    return word;
  }).join(' ');

  return sentence;
}
Enter fullscreen mode Exit fullscreen mode

Solution 2

const reducer = (acc, curr) => {
  const [first, ...rest] = curr.split('');
  const resWord = [...rest,first, 'ay'].join('');
  return acc = acc ? `${acc} ${resWord}` : resWord;
}
const doKata => str => str.split(' ').reduce(reducer, '');
Enter fullscreen mode Exit fullscreen mode

Extra exercise

Try to solve the kata adding the following rule: "leave punctuation marks untouched".

'Pig latin is cool' => igPay atinlay siay oolcay
'Hello world' => elloHay orldway
'Hello, world? How are you, world??!' => elloHay, orldway? owHay eraay ouyay, orldway??!

You can post the solution here as a comment. :)

About this post

I'm am running a free JavaScript Learning Group on [pixari.slack.com] and I use this blog as official blog of the community.
I pick some of the questions from the #questions-answer channel and answer via blog post. This way my answers will stay indefinitely visible for everyone."

If you want to join the community feel free to click here or contact me:

Top comments (1)

Collapse
 
forethought_de profile image
Karthikeyan P • Edited

Hello Raffaele,

Here is my solution :) BTW, I really like, how you solved with array destruction. Well done :)

const strManipulation = (str) => {
  let strArr = str.split(' ');
  const appendStr = 'ay';
  let finalText = "";
  let regex = new RegExp('[^a-zA-Z\d\s: ]', 'g');

  strArr.forEach((item) => {
    let nonAlphabetStartIndex = item.search(regex);
    if(nonAlphabetStartIndex){
        finalText += `${item.slice(1, nonAlphabetStartIndex)}${item.charAt(0)}${appendStr}${item.slice(nonAlphabetStartIndex)} `;
    }
    else{
      finalText += `${item.slice(1)}${item.charAt(0)}${appendStr} `;
    }
  });

  return finalText.trimEnd();

};