This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Dave612
almost 9 years

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??

Answer 554d0522937676c7840000a7

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!";
     ?>
    
points
Submitted by abbott567
almost 9 years

2 comments

drago1147 almost 9 years

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?

drago1147 almost 9 years

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

Answer 553ec67195e378f3340002f7

2 votes

Permalink

I just did this and it let me pass

points
Submitted by Dave612
almost 9 years

2 comments

trock111jomy almost 9 years

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

tozer83 almost 9 years

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.