Created
September 9, 2019 20:02
-
-
Save Twoody/bafc540ebe260b0e4bca9e0f10a26ef2 to your computer and use it in GitHub Desktop.
Code Wars facebook likes implementation using else ifs;
This file contains 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
<?php | |
function likes( $names ) { | |
$l = count($names); | |
if ($l === 0) | |
return "no one likes this"; | |
else if ($l === 1) | |
return $names[0] . " likes this"; | |
else if ($l === 2){ | |
$r = $names[0] . " and " .$names[1]. " like this"; | |
return $r; | |
} | |
else if ( $l === 3 ){ | |
$r = $names[0] . ", " . $names[1] . " and " . $names[2]; | |
$r .= " like this"; | |
return $r; | |
} | |
else{ | |
$diff = $l - 2; | |
$r = $names[0] . ", " . $names[1] . " and " . $diff . " other"; | |
if( $diff !== 1 ) | |
$r .= "s"; | |
$r .= " like this"; | |
return $r; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment