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 #36
DESCRIPTION:
You probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.
Implement the function which takes an array containing the names of people that like an item. It must return the display text as shown in the examples:
[] --> "no one likes this"
["Peter"] --> "Peter likes this"
["Jacob", "Alex"] --> "Jacob and Alex like this"
["Max", "John", "Mark"] --> "Max, John and Mark like this"
["Alex", "Jacob", "Mark", "Max"] --> "Alex, Jacob and 2 others like this"
Note: For 4 or more names, the number in "and 2 others" simply increases.
Starting code:
function likes(names) {
// TODO
}
My attempt:
function likes(names) {
if(names.length===0){
return `no one likes this`
} else if(names.length===1){
return `${names[0]} likes this`
} else if(names.length===2){
return `${names[0]} and ${names[1]} like this`
} else if(names.length===3){
return `${names[0]}, ${names[1]} and ${names[2]} like this`
} else {
return `${names[0]}, ${names[1]} and ${names.length-2} others like this`
}
}
Rank #1 in 'Best Practice':
function likes(names) {
names = names || [];
switch(names.length){
case 0: return 'no one likes this'; break;
case 1: return names[0] + ' likes this'; break;
case 2: return names[0] + ' and ' + names[1] + ' like this'; break;
case 3: return names[0] + ', ' + names[1] + ' and ' + names[2] + ' like this'; break;
default: return names[0] + ', ' + names[1] + ' and ' + (names.length - 2) + ' others like this';
}
}
Rank #2:
function likes(names) {
return {
0: 'no one likes this',
1: `${names[0]} likes this`,
2: `${names[0]} and ${names[1]} like this`,
3: `${names[0]}, ${names[1]} and ${names[2]} like this`,
4: `${names[0]}, ${names[1]} and ${names.length - 2} others like this`,
}[Math.min(4, names.length)]
}
Rank #3:
function likes (names) {
var templates = [
'no one likes this',
'{name} likes this',
'{name} and {name} like this',
'{name}, {name} and {name} like this',
'{name}, {name} and {n} others like this'
];
var idx = Math.min(names.length, 4);
return templates[idx].replace(/{name}|{n}/g, function (val) {
return val === '{name}' ? names.shift() : names.length;
});
}
Rank #4:
function likes(names) {
if(names.length === 0) return "no one likes this";
if(names.length === 1) return names[0] + " likes this";
if(names.length === 2) return names[0] + " and " + names[1] + " like this";
if(names.length === 3) return names[0] + ", " + names[1] + " and " + names[2] + " like this";
return names[0] + ", " + names[1] + " and " + (names.length - 2) + " others like this";
}
Rank #5:
function likes(names) {
switch(names.length){
case 0:
return "no one likes this";
case 1:
return names[0] + " likes this";
case 2:
return names[0] + " and " + names[1] + " like this";
case 3:
return names[0] + ", " + names[1] + " and " + names[2] + " like this";
default:
return names[0] + ", " + names[1] + " and " + (names.length-2) + " others like this";
}
}
Rank #6:
function likes(names) {
names.length === 0 && (names = ["no one"]);
let [a, b, c, ...others] = names;
switch (names.length) {
case 1: return `${a} likes this`;
case 2: return `${a} and ${b} like this`;
case 3: return `${a}, ${b} and ${c} like this`;
default: return `${a}, ${b} and ${others.length + 1} others like this`;
}
}
Troll user !untrue spotted at rank #7:
function likes(peopleNames) {
var feels = new FeelingsParty('like', 'this');
for(var name in peopleNames) feels.addFeeler(new Person(peopleNames[name]));
return feels.shareTheseFeelings();
}
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
function FeelingsParty(emotion, target) {
this.emotionalContext = emotion;
this.emotionalSubject = target;
this.peopleFeelingThisWay = [];
this.numPeopleFeelingThisWay = 0;
}
FeelingsParty.prototype.getEmotionalContext = function() {
return this.type;
}
FeelingsParty.prototype.addFeeler = function(person) {
this.numPeopleFeelingThisWay++;
this.peopleFeelingThisWay.push(person);
}
FeelingsParty.prototype.shareTheseFeelings = function() {
this.findTheRightWords();
if(this.numPeopleFeelingThisWay == 0) return 'no one ' + this.emotionalContext + ' ' + this.emotionalSubject;
if(this.numPeopleFeelingThisWay == 1) return '' + this.peopleFeelingThisWay[0].getName() + ' ' + this.emotionalContext + ' ' + this.emotionalSubject;
if(this.numPeopleFeelingThisWay == 2) return '' + this.peopleFeelingThisWay[0].getName() + ' and ' + this.peopleFeelingThisWay[1].getName() + ' ' + this.emotionalContext + ' ' + this.emotionalSubject;
if(this.numPeopleFeelingThisWay == 3) return '' + this.peopleFeelingThisWay[0].getName() + ', ' + this.peopleFeelingThisWay[1].getName() + ' and ' + this.peopleFeelingThisWay[2].getName() + ' ' + this.emotionalContext + ' ' + this.emotionalSubject;
return '' + this.peopleFeelingThisWay[0].getName() + ', ' + this.peopleFeelingThisWay[1].getName() + ' and ' + (this.numPeopleFeelingThisWay - 2) + ' others ' + this.emotionalContext + ' ' + this.emotionalSubject;
}
FeelingsParty.prototype.findTheRightWords = function() {
if(this.numPeopleFeelingThisWay == 0 || this.numPeopleFeelingThisWay == 1) this.emotionalContext += 's';
}
Felt like I stumbled onto a cleanliness expo and I'm hauling in a garbage truck while looking at the better solutions. 🤣
That's it for Kata #36, 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 #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 #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