Functions Part 1 - Exercise 8


#1

This is my code for exercise 8. I passed the lesson but I had to assign the lucky winner. I had to print out that Julia is the lucky winner. I thought the rand() is supposed to pick out a lucky winner...?

Looking forward to your feedback! :grimacing:

<?php
// Create an array and push on the names
// of your closest family and friends

**$friends = array();**
**array_push ($friends, "Brad");**
**array_push ($friends, "Scottie");**
**array_push ($friends, "Leo");**
**array_push ($friends, "Julia");**
**print count($friends);**

// Sort the list
sort($friends);
print join(",", $friends);

// Randomly select a winner!
rand();

// Print the winner's name in ALL CAPS
strtoupper("Julia");
print("Julia");
?>


#2

rand() is going to generate a random integer for you.

The PHP manual:
https://secure.php.net/manual/en/function.rand.php

rand ( int $min , int $max )

You have an array with 4 items so see if you can think of away to use:

rand(0,3)

#3

It outputs 4Brad,Julia,Leo,ScottieJulia


#4

The issue was the print join and the print count. Now it just delivers Julia as the lucky winner. :smile:


#5

It doesn't sound like you did this, unless Julia is really lucky:

print strtoupper($friends{rand(0,3)});

#6

Here is the correct code, in case that it might help out other users:

<?php
// Create an array and push on the names
// of your closest family and friends
$friends = array();
array_push ($friends, "Brad");
array_push ($friends, "Scottie");
array_push ($friends, "Leo");
array_push ($friends, "Julia");
// Sort the list
sort($friends);
// Randomly select a winner!
rand(0,3);
// Print the winner's name in ALL CAPS
strtoupper("Julia");
print("Julia");
?>


#7

I'm not buying a ticket in your lottery! :smile:


#8

Are you sure? You might regret it later! :wink:


#9

2 posts were split to a new topic: This is what i got


#11

Hey! I just found out another way to randomly select a winner :smile:

   // Randomly select a winner!
    	$winner = $friends[rand(0, count($friends)-1)]; 
        print strtoupper ($winner);

You have to remember that array elements are found with square brackets... Hope it would be useful for all of you :wink:


#12

5 posts were split to a new topic: 8. Remember to print out the name of the lucky winner!


#16

Can you please explain me why it's sorting/rsorting that way? And why array need to be sorted with a random selection? I can't wrap my head around it. Sorry for my english in advance. Not a native language.

 $friends = array(); 
 array_push($friends, "Joey");
 array_push($friends, "Monica");
 array_push($friends, "Chandler");
 array_push($friends, "Phoebe");
 array_push($friends, "Ross");
 array_push($friends, "Rachel");
    
    print "Lottery begins. The contestants are... <br> <br>";
    print_r ($friends);
    print "<br><br>";
    print join ($friends, "<br>");
    
    sort ($friends);
    print "<br><br>";
    print_r ($friends);
    print "<br><br>";
    print join ($friends, "<br>");
    
    rsort ($friends);
    print "<br><br>";
    print_r ($friends);
    print "<br><br>";
    
print join ($friends, "<br>");
sort ($friends);
print " <br><br> And the winner is ... ";
print strtoupper ($friends[rand (0, count ($friends) - 1)]);

#17

4 posts were split to a new topic: 8. Why do we need to sort the list


#26