Skip to content

Instantly share code, notes, and snippets.

@jnewton00
Created October 4, 2021 14:44
Show Gist options
  • Save jnewton00/8f03475c4c794c40e1314bf7c30680b9 to your computer and use it in GitHub Desktop.
Save jnewton00/8f03475c4c794c40e1314bf7c30680b9 to your computer and use it in GitHub Desktop.
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"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WhoLikesIt
{
class Program
{
static void Main(string[] args)
{
string[] name = { "Alex", "Jacob", "Mark", "Max" };
Kata.Likes(name);
Console.WriteLine(Kata.Likes(name));
Console.ReadLine();
}
public static class Kata
{
public static string Likes(string[] name)
{
string NoLikey = "no one likes this";
string LikeThis = " like this";
string LikesThis = " likes this";
string Results;
foreach (var item in name)
{
if (String.IsNullOrEmpty(item))
{
Results = NoLikey;
return Results;
}
else if (name.Count() == 1)
{
Results = name[0] + LikesThis;
return Results;
}
else if (name.Count() == 2)
{
Results = name[0] + LikeThis;
return Results;
}
else if (name.Count() == 3)
{
Results = name[0] + ", " + name[1] + " and " + name[2] + LikeThis;
return Results;
}
else if (name.Count() == 4)
{
Results = name[0] + ", " + name[1] + " and " + (name.Count() - 2) + " others " + LikeThis;
return Results;
}
else
{
return "null";
}
}
return Results;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment