Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created March 31, 2022 04:05
Show Gist options
  • Save natafaye/ffced5d0b1f672bfcf640e8bebc087fe to your computer and use it in GitHub Desktop.
Save natafaye/ffced5d0b1f672bfcf640e8bebc087fe to your computer and use it in GitHub Desktop.
// We ran out of time, but here's one way we could have refactored our code if we wanted it to be more customizable
// Line 17 uses a ternary operator, which is somethign we'll be using more and more going forward!
function likes(names) {
let verb = "like";
if(names.length < 2) verb += "s";
nameList = names[0];
if(names.length === 0) {
nameList = "no one"
}
else if(names.length === 2) {
nameList += " and " + names[1];
}
else if(names.length > 2) {
let others = (names.length > 3) ? (names.length - 2) + " others" : names[2];
nameList += ", " + names[1] + " and " + others;
}
return nameList + " " + verb + " this";
}
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"
}
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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment