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

Functions, Part I Forum

View Course » View Exercise

162 points
547e8ae152f863d8400008e3_58733793
Submitted by
Dave612
about 2 years ago

I keep getting "Did you remember to use 'Array()'?" [8/8]

I don't know what I'm doing wrong?? The code works, but the site won't accept it??

<?php
$friends = array("Galen","Grace","Henry","Maria");
sort($friends);
$howmanyfriends = count($friends) - 1;
$randfriend = rand(0,$howmanyfriends);
print strtoupper($friends[$randfriend]);
print " WINS!!!!";
?>


3 votes

permalink

If anyone else finds this and wants an explanation, here is one =)

1) $names is set as an empty array, then the friends names are pushed into it.

2) $names is sorted and then counted, the number of names is assigned to $amount. In this case, $amount will be set to 3, as there are 3 names in the array.

3) $rand is assigned to whatever number is spat out when you randomise $amount. So, in this example, we run rand(0, $amount); this will give us a random number between 0 and 3 and assign it to the $rand variable. Lets imagine $rand is now set to 2.

4) The last line prints out the second entry in the array. As in our example $rand was 2, so $names[$rand] in this example is the same as $names[2]. This is then wrapped in the function strtoupper() to make it all uppercase, and the . " " . "WINS!" bolted onto the back of this completes the exercise.

    <?php
    // Create an array and push on the names
    // of your closest family and friends
    $names = array();
    array_push($names, "James", "Rick", "Ross");

    // Sort the list
    sort($names);
    $amount = count($names);

    // Randomly select a winner!
    $rand = rand(0, $amount);
    echo strtoupper($names[$rand]) . " " . "WINS!";
    ?>

463 points
624f67643bdf37b458085aee294c870d?s=140&d=retro
Submitted by
abbott567
about 2 years ago

2 Comments

4d5ac8b28e69b92d6096a0565b6a4f00?s=140&d=retro drago1147 about 2 years ago

Hello,
If we take out the requirement to use the functions listed in the exercise, would the code below be doing what your code does?
<?php
$people = array("rick","james","george","stephen","pascal");
$randperson = arrayrand($people);//picks a single random key from the array
$winner = $people[$rand_person]; //converts the random key into its value
print strtoupper($winner) . " " . "WINS";
?>

4d5ac8b28e69b92d6096a0565b6a4f00?s=140&d=retro drago1147 about 2 years ago

Well the code was converted to single line, was not posted like this


2 votes

permalink

I just did this and it let me pass

<?php
$friends = array("Galen","Grace","Henry","Maria");
//array()
//array_push()
sort($friends);
$howmanyfriends = count($friends) - 1;
$randfriend = rand(0,$howmanyfriends);
print strtoupper($friends[$randfriend]);
print " WINS!!!!";
?>

162 points
547e8ae152f863d8400008e3_58733793
Submitted by
Dave612
about 2 years ago

2 Comments

76549b36fdd817b4f940226127ef84b2?s=140&d=retro trock111jomy about 2 years ago

I just copy pasted this one and it worked for me too.. but i don't know why though ? or why my code hasn't worked

527be8e5abf821589f00145e_829741181 tozer83 almost 2 years ago

I believe it's because the site is looking for various keywords to make sure you're using everything you've learned. If you original code didn't include 'array_push' for example then it would not count as a success.