This forum is now read-only. Please use our new forums! Go to forums
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
If anyone else finds this and wants an explanation, here is one =)
$names is set as an empty array, then the friends names are pushed into it.
$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.
$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.
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!"; ?>
Answer 553ec67195e378f3340002f7
2 comments
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
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.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
2 comments
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?
Well the code was converted to single line, was not posted like this