functionlikes(...names){constadjective=names.length>1?'like':'likes';constconcatenator='and';constmaxNames=3;constremainingCount=names.length-maxNames;constremainingCaption=remainingCount===1?'other':'others';// Empty listif(!names.length){names=['No one'];}constoxfordComma=names.length>2?',':'';constlastPerson=remainingCount>0?`${remainingCount}${remainingCaption}`:names.pop();constpeople=names.length===0?lastPerson:`${names.slice(0,maxNames).join(', ')}${oxfordComma}${concatenator}${lastPerson}`;constoutput=`${people}${adjective} this`;console.log(output);}likes();likes('Jack');likes('Jack','Jill');likes('Jack','Jill','Bill');likes('John','Paul','George','Ringo');likes('John','Paul','George','Ringo','Archie');likes('John','Paul','George','Ringo','Archie','Annie','Jeff','Abed');// "No one likes this"// "Jack likes this"// "Jack and Jill like this"// "Jack, Jill, and Bill like this"// "John, Paul, George, and 1 other like this"// "John, Paul, George, and 2 others like this"// "John, Paul, George, and 5 others like this"
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Discussion on: Day 1: Who likes it? - A coding challenge with solutions
Replies for: Nice solution. This does not say "and x others" though.
For further actions, you may consider blocking this person and/or reporting abuse