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