8 show what you know!


#1

Could someone please give me the correct code to "show what you know"? Thanks!


#2

No. This is a learning community. We do not share working code for the exercises.


#3

I have been stuck on this for three hours now. Could you at least tell me how to choose a random person?


#4

I can help lead you to the answer.

Please post your code here, and the exercise instructions.


#5

My code:

    <p>
	<?php
	// Create an array and push on the names
    // of your closest family and friends
    $friends = array("Luke", "Liam", "william", "Niko");
	// Sort the list
sort($friends);
	// Randomly select a winner!
    
	// Print the winner's name in ALL CAPS
	?>
	</p>

The instructions:

Create an array() with the names of your closest family and friends. The more names in your array, the more fun your game will be!

After you've created your list, sort it and randomly select a name from the list.

When you have your winning name, print it to the screen in caps so everyone knows how awesome the winner is.

Functions we'll use here: array(), array_push(), sort(), count(), rand(), and strtoupper(). Play around with some of the other functions you've learned as well.


#6

To randomly select a person, you can use the PHP rand() function which takes two arguments, min and max.

rand(min, max): for example: rand(0,15) will spit out a random number between 0 and 15 inclusive of 15.

So, with this info, how could you count() the members of the array? (Hint: it's count()).

Now that I've explained how rand() works, how can you get a random selection from the array?

Keep in mind arrays work from 0 up with 0 being the first member.


#7

Like this?

rand(0, count($friends)-1);


#8

Never mind, I figured it out.


#9

Hello.... I have been having a similar problem to yours and have tried different approaches to solve it but i couldnt. This is my code. Plz help
// of your closest family and friends
$family_friends = array();
array_push($family_friends, "Badmus");
array_push($family_friends, "Temidayo");
array_push($family_friends, "Oluwatosin");

// Sort the list

$sort = sort($family_friends);
// Randomly select a winner!
$rand = rand(0, count($sort)-1);
// Print the winner's name in ALL CAPS
$upper = strtoupper ($rand[$sort]);
print $upper;
?>


#10

Change $upper = strtoupper ($rand[$sort]); to $upper = strtoupper ($friend[$sort]);



<?php
// Create an array and push on the names
// of your closest family and friends
$friend=array();
array_push($friend, "armus");
array_push($friend, "gadus");
array_push($friend, "parus");
// Sort the list
$sort=sort($friend);

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

	// Print the winner's name in ALL CAPS
	$upper = strtoupper ($friend[$sort]); 
    print $upper;
	?>
	</p>
</html>

#11

How? To print the array, it only takes an integer [1] value, but I want it to read a $variable that is an int, but it won't let me?

$qtyOfEntries = count($entries);
$randomSelection = rand(0, $qtyOfEntries);
$winner = $entries[$randomSelection];

#12

Found the answer on another post. Confusing me a bit to how this is the answer as rand() works on min & max, and here max = (count($entries) - 1). So if say this is count gives a sum of 5, then why does it -1 on the end of this?

$winner = $entries[rand(0,(count($entries) - 1))];


#13

This worked for me...

<?php
// Create an array and push on the names
// of your closest family and friends
$player_arr = array();
array_push($player_arr, "Dan");
array_push($player_arr, "Joe");
array_push($player_arr, "Jon");
array_push($player_arr, "Frank");

// Sort the list
sort($player_arr);

// Randomly select a winner!
$count = count($player_arr);
$rand = rand(0, $count - 1); // from 0 to 6

// Print the winner's name in ALL CAPS
print strtoupper($player_arr[$rand])
?>

#14

This works for me though.

<?php
// Create an array and push on the names
$frameworks = array('laravel', 'angular', 'lumen');

	array_push($frameworks, 'react');

	// Sort the list
   sort($frameworks);

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

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


#15

it gives an error that. remember to print lucky winner, code is running but the compiler doesn't accepting it.


#16

you code is correct. that was my mistake, am not including some other segments


#17



<?php
// Create an array and push on the names
// of your closest family and friends
$myarray = array();
array_push($myarray, "Emine");
array_push($myarray, "Cansu");
array_push($myarray, "Sevgi");
array_push($myarray, "Sara");
array_push($myarray, "Bijen");
// Sort the list
$sortiert = sort($myarray);
// Randomly select a winner!
$zf = rand(0, count($sortiert-1));
// Print the winner's name in ALL CAPS
print strtoupper($myarray[$zf]);
?>


Well, that worked for me. :slightly_smiling:


#18



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

// Sort the list

 sort ($family);
 
 print (strtoupper($family[rand(0,count($family))]));
// Randomly select a winner!

// Print the winner's name in ALL CAPS

?>
</p>


#19

Hi there,

hope this isn't offtopic: Could someone explain that rand(0, count($variable) -1); part to me? I still don't understand why the last element of an array/string is indexed at -1. I must have overlooked something in an earlier excercise.


#20

Hi,
Arrays are zero-indexed, meaning that the first element is at position 0 (zero) and the last element is at count($array) - 1. This means it counts your array elements and -1 from length. example if you have 3 elements in your array then its index is started from zero, i.e your 1st element is at position 0, 2nd element is at position 1, and 3rd element is at position 2.
This worked for me.

`


<?php
// Create an array and push on the names
// of your closest family and friends
$ff = array();
array_push($ff, "Mom");
array_push($ff, "dady");
array_push($ff, "Halima");
array_push($ff, "Mosooma");
array_push($ff, "Javied");
array_push($ff, "faried");
array_push($ff, "Ali");
array_push($ff, "Razia");
array_push($ff,"Golsum");
array_push($ff, "Rahima s");
array_push($ff, "fatima");
// Sort the list
$sort=sort($ff);

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

// Print the winner's name in ALL CAPS
$upper = strtoupper($ff[$rand]);
print $upper;
?>
</p>

`