Everyone Can Code!

JavaScript Kata #25: Are You Playing Banjo?


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 #25

DESCRIPTION:

Create a function which answers the question "Are you playing banjo?".
If your name starts with the letter "R" or lower case "r", you are playing banjo!

The function takes a name as its only argument, and returns one of the following strings:

name + " plays banjo" 
name + " does not play banjo"

Names given are always valid strings.

Starting code:

function areYouPlayingBanjo(name) {
  // Implement me
  return name;
}

My attempt:

const areYouPlayingBanjo = name => name[0] == "R" || name[0] == "r" ? `${name} plays banjo` : `${name} does not play banjo`

One-liners are so satisfying.

Rank #1 in 'Best Practice':

function areYouPlayingBanjo(name) {
  return name + (name[0].toLowerCase() == 'r' ? ' plays' : ' does not play') + " banjo";
}

Rank #2:

function areYouPlayingBanjo(name) {
  if (name[0].toLowerCase() === 'r') {
    return name + ' plays banjo';
  } else {
    return name + ' does not play banjo';
  }
}

Rank #3:

function areYouPlayingBanjo(name) {
  // Implement me
  if (name[0] == 'R' || name[0] == 'r')
    return name + " plays banjo";
  else
    return name + " does not play banjo";
}

Rank #4 with what I would assume to be RegEx:

function areYouPlayingBanjo(name) {
  return name + (/^r/i.test(name) ? " plays " : " does not play ") + "banjo";
}

Rank #5:

function areYouPlayingBanjo(name) {
  return name[0].toLowerCase() == "r" ? name + " plays banjo" : name + " does not play banjo";
}

A top troll with all the time in the world in the form of user !untrue at rank #6:

function areYouPlayingBanjo(n)
{ 
  // Imaginate a new Person
  var theCandidate = new Person(n);
  
  // Invoke the power of Instrumentology
  var theRitual = new Instrumentology();
  theRitual.beginAudit(theCandidate);
  
  // See if the Person now plays the banjo
  return theCandidate.queryInstrument("banjo");
}


function Person(name) {
  this.name = name;
  this.instrumentsPlayed = [];
}

Person.prototype.getFirstLetterOfName = function() {
  if(this.name.length > 0) {
    var firstLetter = this.name.substr(0,1);
    firstLetter = firstLetter.toLowerCase();
    return firstLetter;
  } else {
    return false;
  }
}

Person.prototype.getName = function() {
  return this.name;
}

Person.prototype.queryInstrument = function(instrument) {
  for(var i = 0; i < this.instrumentsPlayed.length; i++) {
    if(instrument == this.instrumentsPlayed[i].getName()) 
      return this.getName() + " plays " + instrument;
  }
  return this.getName() + " does not play " + instrument;
}

Person.prototype.learnInstrument = function(instrument) {
  if(instrument instanceof Instrument) {
    this.instrumentsPlayed.push(instrument);
  }
}

function Instrumentology() {
  this.theBookOfInstrumentology = [
    { letter: "a", instrument: new Instrument("flute") },
    { letter: "b", instrument: new Instrument("guitar") },
    { letter: "c", instrument: new Instrument("bongos") },
    { letter: "r", instrument: new Instrument("banjo") },
    { letter: "s", instrument: new Instrument("tambourine") },
    { letter: "z", instrument: new Instrument("triangle") }];
}


Instrumentology.prototype.beginAudit = function(person) {
  person.learnInstrument(this.consultTheSacredTexts(person.getFirstLetterOfName()));
}

Instrumentology.prototype.consultTheSacredTexts = function(telemetry) {
  for(var i = 0; i < this.theBookOfInstrumentology.length; i++) {
    if(this.theBookOfInstrumentology[i].letter == telemetry) {
      return this.theBookOfInstrumentology[i].instrument;
    }
  }
}


function Instrument(name) {
  this.name = name;
}

Instrument.prototype.getName = function() {
  return this.name;
}

Not sure why but suddenly these joke-codes feel like someone telling a joke but the punchline comes way too long that you manage to fall asleep, wake up and still wait for the joke to finish... but maybe that's just me and my different tastes in jokes. ¯\_(ツ)_/¯

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

Resources

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

How do you rate this article?

4


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.