This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Functions, Part I Forum

View Course » View Exercise

93 points
Picture
Submitted by
Markx0
almost 2 years ago

Working solution for - "Show What You Know" 8/8

<html>
<p>
<?php
// Create an array and push on the names
// of your closest family and friends
$friends = array ();
array_push($friends, "Ezra");
array_push($friends, "Diana");
array_push($friends, "Bartok");
array_push($friends, "Mully");
array_push($friends, "Tommy");
array_push($friends, "Erika");

// Sort the list
sort($friends);

// Randomly select a winner!
$random = rand(0,count($friends) -1);
$namewinner=join($friends);
$winner = $friends[rand(0,5)];
// Print the winner's name in ALL CAPS
print strtoupper($winner)
?>
</p>

2 votes

permalink

For some reason, I found that just randomizing the name selection output a null response once in a while — although this could just be a Codecademy bug. In any case, I fixed it by rounding the randomized output:

$random = round(rand(0, count($friends) - 1));

643 points
9ccc853d21d17885fa2357cbd711ed3e?s=140&d=retro
Submitted by
Tim Samoff
almost 2 years ago

1 Comment

A5ba1285e8142aa3740cc08378e41d6c?s=140&d=retro Reianna-Bryant over 1 year ago

Your code was the only one that worked for me. Thank you!


2 votes

permalink

<?php
// Create an array and push on the names
// of your closest family and friends
$friends = array();
arraypush($friends ,"Jumanji");
array
push($friends ,"Apita");
array_push($friends ,"Anjo");
// Sort the list
sort($friends );
// Randomly select a winner!

$winner = $friends[rand(0,count($friends) - 1)];
// Print the winner's name in ALL CAPS
print strtoupper($winner);
?>
</p>

534 points
E44d2bf7da7c4bdb6cfe4618adfe6670?s=140&d=retro
Submitted by
Jehnee
over 1 year ago


0 votes

permalink

I found it, please try it.


<?php
// Create an array and push on the names
// of your closest family and friends
$family = array ();
arraypush($family, "lulu");
array
push($family, "putri");
arraypush($family, "hamzah");
array
push($family, "nia");
array_push($family, "hidayah");

// Sort the list
sort($family);

// Randomly select a winner!
$random = rand(0, count($family) - 1);

// Print the winner's name in ALL CAPS
print strtoupper($family[$random]);
?>
</p>

691 points
558a5cf59376768f19000498_18583614
Submitted by
Ahmad Sapuan
almost 2 years ago


0 votes

permalink

i dont get the

$namewinner=join($friends)

why do you do a 'join' there?

245 points
1e381bffef7886fbbf0239dee0b235a1?s=140&d=retro
Submitted by
Tobi
almost 2 years ago

1 Comment

71561a50833386872fa9bca137ea463c?s=140&d=retro Steve over 1 year ago

This line is not needed. Then again neither is the sort.