Skip to content

Instantly share code, notes, and snippets.

@Twoody
Created September 9, 2019 20:02
Show Gist options
  • Save Twoody/bafc540ebe260b0e4bca9e0f10a26ef2 to your computer and use it in GitHub Desktop.
Save Twoody/bafc540ebe260b0e4bca9e0f10a26ef2 to your computer and use it in GitHub Desktop.
Code Wars facebook likes implementation using else ifs;
<?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