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

Functions, Part I Forum

View Course » View Exercise

166 points
8d9dcf79ebb2cd0d3440eb421178503d?s=140&d=retro
Submitted by
Jccxx01
almost 3 years ago

need help with my code on 8/8

<?php
// Create an array and push on the names
// of your closest family and friends
$contestants = array();
arraypush("Marc");
array
push("Rayven");
arraypush("TJ");
array
push("Blake");
arraypush("Quin");
array
push("Chase");
arraypush("Mindy");
array
push("Caroline");
// Sort the list
sort($contestants);
// Randomly select a winner!
count($contestants) - 1;
$rand = rand(0,$count);
$upper = $contestants[$rand];
// Print the winner's name in ALL CAPS
print strtoupper[$winner];
?>


this is the code I'm trying to use but it won't accept it please help


4 votes

permalink

Issues:

  1. Change all of your arraypush("Name"); to array_push(array, "Name");
  2. You also didn't define $count. You'll need to add that: $count = count($contestants) - 1;
  3. You've also not defined winner. You've defined a variable called upper instead.
  4. You used [] instead of () in your final strtoupper.

    <?php
    // Create an array and push on the names
    // of your closest family and friends
    $contestants = array();
    arraypush($contestants, "Marc");
    array
    push($contestants,"Rayven");
    arraypush($contestants,"TJ");
    array
    push($contestants,"Blake");
    arraypush($contestants,"Quin");
    array
    push($contestants,"Chase");
    arraypush($contestants,"Mindy");
    array
    push($contestants,"Caroline");
    // Sort the list
    sort($contestants);
    // Randomly select a winner!
    $count = count($contestants) - 1;
    $rand = rand(0,$count);
    $winner = $contestants[$rand];
    // Print the winner's name in ALL CAPS
    print strtoupper($winner);
    ?>

664 points
Picture
Submitted by
Gaius Augustus
almost 3 years ago

3 Comments

Picture Gaius Augustus almost 3 years ago

You also should probably go ahead and store your sorted contestants into a variable with: $sorted = sort(contestants); and then use $sorted from there down when calling the array.

8d9dcf79ebb2cd0d3440eb421178503d?s=140&d=retro Jccxx01 almost 3 years ago

thanks it worked

Ed59660596a99c1483041cfc3e4bbf3f?s=140&d=retro BrianBriu almost 3 years ago

<?php

//Create the array.
$names = array();

//Insert the words into the array.
array_push($names, "Name1", "Name2", "Name3", "Name4", "Name5", "Name6"); 

//Sort the array.
sort($names);

//Count the elements in the array.
$c = count($names);

//Elements in the array - 1. Because counting start at 0.
$cm = $c - 1;

//Randomize the elements numbers and chose the winner. Every element has a number.
$r = rand(0, $cm);

//Make the winners name UPERCASE. Don't forget that you select an element from an array using $array[number].
$winner = strtoupper($names[$r]);

//Echo the winner!
echo "The winner is ".$winner;
?>

4 votes

permalink

    <?php

    //Create the array.
    $names = array();

    //Insert the words into the array.
    array_push($names, "Name1", "Name2", "Name3", "Name4", "Name5", "Name6"); 

    //Sort the array.
    sort($names);

    //Count the elements in the array.
    $c = count($names);

    //Elements in the array - 1. Because counting start at 0.
    $cm = $c - 1;

    //Randomize the elements numbers and chose the winner. Every element has a number.
$r = rand(0, $cm);

//Make the winners name UPERCASE. Don't forget that you select an element from an array using $array[number].
$winner = strtoupper($names[$r]);

//Echo the winner!
echo "The winner is ".$winner;
    ?>

414 points
Ed59660596a99c1483041cfc3e4bbf3f?s=140&d=retro
Submitted by
BrianBriu
almost 3 years ago

2 Comments

532afe767c82caf100002734_506113534 Thinnling over 2 years ago

Thanks. Very clear and easy to understand when coded this way.

133d4fee9673866ef2ea30d5f299177b?s=140&d=retro ELIEL DIAZ GONZALEZ over 2 years ago

its not working for me