Created
March 31, 2022 04:05
-
-
Save natafaye/ffced5d0b1f672bfcf640e8bebc087fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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