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

Functions, Part I Forum

View Course » View Exercise

432 points
Picture
Submitted by
nonameboy7
over 2 years ago

5/8 my random letter from my name program works but they won't accept it

my program works. it prints a random letter from my name. but they won't let me move on, its so frustrating. thoughts?

<?php
// Use rand() to print a random number to the screen
print rand();

?>
</p>
<p>
<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.

$name = "chance";

echo strlen($name);
echo "
";
$part = substr($name, rand(0,5));


switch ($part) {
case "chance": echo "c";
break;
case "hance": echo "h";
break;
case "ance": echo "a";
break;
case "nce":echo "n";
break;
case "ce": echo "c";
break;
case "e": echo "e";
break;
default: echo "Not Chance";
break;
}
?>
</p>

3 votes

permalink

Try replacing the whole thing (even the variable they tell you to make) with this:

print substr("chance", rand(0,5), 1);

it accepted that as an answer, also would work with the variable if you want to include it.

$name = "chance";
print substr($name, rand(0,5), 1);

790 points
12dd377868f46293c0d74f95ce797aae?s=140&d=retro
Submitted by
lylelewtongmail.com
over 2 years ago

1 Comment

Picture Morgan Kail-Ackerman over 2 years ago

It keeps telling me that the answer is not an integer…. know how to solve this?


1 vote

permalink

Use of Switch is not needed.
Here is my solution

$myName = "Ahmad";
$length = strlen("Ahmad");
$newlength = $length - 1;
$random = rand(0, $newlength);
print substr($myName, $random, 1);

545 points
283e4cfe624216454c4940c1108e1605?s=140&d=retro
Submitted by
Hero0o
over 2 years ago

1 Comment

C195f1bbf40fd68578563e5d167fdcee?s=140&d=retro Sapna Kumar about 2 years ago

worked for me. Thank you.