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

Functions, Part I Forum

View Course » View Exercise

855 points
Picture
Submitted by
Mohammed Fathy Alhedhed
over 2 years ago

what is the wrong with my code

<?php

**//Create the array.**
$array = array();
//Insert the words into the array.
array_push($array, "Name1", "Name2", "Name3", "Name4", "Name5", "Name6"); 
**//Sort the array.**
sort($array);

//Make the winners name UPERCASE. Don't forget that you select an element from an array using $array[number].
$r = rand(0, count($array) - 1);
$winner = strtoupper($array[$r]);
print $winner;
?>


2 votes

permalink

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

    $AzumaF = array();
    array_push($AzumaF, "Ayame");
    array_push($AzumaF, "Rikimaru");
    array_push($AzumaF, "Tatsumaru");
    array_push($AzumaF, "Rin");
    array_push($AzumaF, "Semimaru");
    array_push($AzumaF, "Kiku");
    array_push($AzumaF, "Shiunsai");

// Sort the list

    $AzumaS = sort($AzumaF);


// Randomly select a winner!

    $AzumaR = count(rand(0,$AzumaS)-1);


// Print the winner's name in ALL CAPS

    print strtoupper($AzumaF[$AzumaR]);

?>

Hope this example can help!

240 points
Ae220345a664ff3a4393c49ef3075404?s=140&d=retro
Submitted by
KattKriger
over 2 years ago

1 Comment

51bcce8c7c82cad662011ce4_302638316 celso341 over 2 years ago

thanks so much!!


1 vote

permalink

I am sorry but i think there is no problem in the code...every thing is correct....

229 points
677d1dfae58d8a186f49b70b60a82a93?s=140&d=retro
Submitted by
Vamsi Krishna
over 2 years ago


1 vote

permalink

sorry @KattKriger, but there are 2 things wrong with the code above:

1) $AzumaS will always be 1 because sort() returns 1, not the sorted array.
2) $AzumaR will always be always be 1 because you are counting either 0 or -1, not the length of the array.

written correctly it should be:
sort($AzumaF);
and
$AzumaR = rand(0, count($AzumaF) -1);

while your code will work, your winner will always be RIKIMARU

324 points
Picture
Submitted by
David Fudge
over 2 years ago


0 votes

permalink

i'm having the same problem what should i do??

547 points
55e7642086f55250390004dc_31640069
Submitted by
MissPotato201
over 2 years ago


0 votes

permalink

thanks so much!!!:)

547 points
55e7642086f55250390004dc_31640069
Submitted by
MissPotato201
over 2 years ago


0 votes

permalink

Always a pleasure! :)

240 points
Ae220345a664ff3a4393c49ef3075404?s=140&d=retro
Submitted by
KattKriger
over 2 years ago