This forum is now read-only. Please use our new forums! Go to forums
Oops, try again! The random output you printed from your name should be just one character
I don’t know why my code is not working. I just receive this warning: Oops, try again! The random output you printed from your name should be just one character. But I can see the correct result on the console. I have spent a couple of hours and I don’t know. Thanks for your help.
<?php
// Use rand() to print a random number to the screen
echo "A number between 2 and 10 is ";
echo rand(2, 10);
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "Benjamin";
echo "A character number of $name is of ";
echo strlen ($name);
echo "The name characters for $name between the position of 2 and 4 is ";
echo substr($name, 2, 4);
$random = rand(0, strlen ($name)-1);
$alea = substr($name, $random, 1);
//echo substr($name, $random, $random1];
echo $name[$alea];
?>
Answer 515ce3155705370d4d0007e2
I spent much time on this problem and this is what I came up with. I remembered that rand()
requires two parameters and substr()
takes three. So you have to think. You’re looking for one character in your name – which you should store in a variable I.E. $myName
– so you want to use substr()
first. Remember that this takes three parameters. The first parameter in substr()
represents the actual string. So if your stored your name in a variable, it would be
$answer = substr(variable, ..., ...);
. The next parameter is the starting point. Remember that substr()
treats characters in a string as a zero-indexed array. What this means is that the first letter in your string (which should be stored in a variable) is positioned at zero. So that’s where we want to start our random character. So you want to put rand(0, ...)
inside of your substr()
so now we should have $answer = substr(variable, rand(0, ...), ...);
Now for the second parameter of rand()
(because it does take two) we need to have the maximum number. If we think about it, the final letter in your string will be the length minus 1. This is because with a zero-indexed array, each letter is minus 1 from where you’d count it at. So in my name – Chris – there are 5 letters. In an array, the numbers would be positioned at array[0, 1, 2, 3, 4];
So yes, strlen()
will return 5, but with substr()
in there, the final letter would be at 5 - 1. So if you followed all of that, now we should be at$answer = substr(variable, rand(0, strlen(variable)-1, ...)
. And finally, the final parameter in our substr()
function. The final parameter is reserved for how many characters you’d like to return. This lesson is asking for only one, so it is safe to assume that we’re going to put a one there. The final code you should end up with should look something similar to
$answer = substr(variable, rand(0, strlen(variable)-1, 1);
Then of course you want to print/echo it, so after the semi-colon, put echo $answer;
. My final code looked like this:
<html>
<p>
<?php
//Use rand() to print a random number to the screen
echo floor(rand(5, 15));
?>
</p>
<p>
<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "Chris";
$print = substr($name, rand(0, strlen($name) - 1), 1);
echo $print;
?>
</p>
Answer 515c9c0e71279fdbc30000bc
Simplify! The fewer lines of code, the better.
<html>
<p>
<?php
// Use rand() to print a random number to the screen
print rand(1, 10);
?>
</p>
<p>
<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$myName = "Josiah";
$sub1 = substr($myName, rand(strlen($myName) - 1), 1);
print $sub1;
?>
</p>
1 comments
Hi Josiah, rand() should have 2 parameters, maybe you lack comma after strlen($myName) =)
Answer 515b1173e8a240ceb50023af
Spanish:Hola Benjamin, gracias a tu codigo he conseguido que funcione el mio, yo lo hice asi: English: Hi Benjamin, thanks to your code I got mine working fine, here it is:
2 comments
Hi Bruno. Good eye to know I speak Spanish :) Unfortunately, your code isn’t work on my console neither. I don’t know what’s go on.. I tried in other browsers as well but it doesn’t work
Bruno muchas gracias por la solucion hermano, por mas vueltas que le daba no encontraba la forma de solucionar el ejercicio.
Answer 521daf3fabf8212bc7001e10
Answer 515be98272b62d5148000149
2 comments
It’s my same problem and it everything fine apparently
Try to refresh adriano…press ctrl and r or command and r simultaniously…is a bug there in the codeacademy editor and sometimes dont check right the code…if u still having problems please tell me
Answer 51625f973bb2a8367b000a00
Thanks for the explanation, was very helpful, but i have a small misunderstanding. I writte my code like this:
Answer 5262ea1df10c60822300b51d
Hi all, this exercise requires the use of strlen(), substr() and rand(). However a simple, clean and working solution that doesn’t involve substr() is:
<html>
<p>
<?php
// Use rand() to print a random number to the screen
print rand(0,5);
?>
</p>
<p>
<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "myname";
print ($name[rand(0, strlen($name-1))]);
?>
</p>
1 comments
its working..
Answer 5279d82aabf8210e18000ac9
Answer 5332eafb548c357886004eaa
Answer 515c86f91d70a483ba0005d7
thanks for the help, I was kinda close, yet so far away :)
6 comments
still the problem is same as above after refreshing also
use this: print ($name[rand(0, strlen($name-1))]);
The output on the console is true but then also it displays the error.
Write your code down so I can check it; do it in a post and not in a comment please
Check the post, I have posted it
can’t see you post yet. sometimes it takes time to show. In the meanwhile check the solution I’ve posted yesterday on the bottom of this thread
Answer 515ca90c9b8939597800030d
Benjamin…solucionastes el problema? Si no es asi dime algo a [email protected] e intentare ayudarte en todo lo posible. pd:me gusto mucho tu web
1 comments
Gracias Bruno. Si lo acabo de solucionar tras entender la gran explicacion que Chriss Mackay me ha hecho. Mucho animo. Imagino que estas enganchado a codecademy como yo :)
Answer 52fb993680ff3339a0002a57
Answer 53daa906631fe94a3800043e
I have the following code and it does not work on the editor
Answer 54420348282ae3b07e0010c2
SOrry none of the answers help in the case…this is the msg i am getting Oops, try again. Your printed output from rand() should be an integer.
1 comments
thats the correct answer just try this’
Answer 546ee3f57c82ca31300014e5
1 comments
this is correct answer for printing the random character
Answer 54ce7a0195e3787208003819
Answer 554fb0d6d3292f26c00001a1
Simply do :-
$name = “Pravvy”; $len = strlen($name); $rand = rand(0, $len - 1); echo $name[$rand];
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
35 comments
That’s great. It worked!! :) Thanks
Great answer with a great explanation. Thanks.
No problem, Glad I could help!
Fantastic explanation, thanks!
No problem! :)
thanks mate, this was very helpful. btw, is it just me or does anyone have problems with this site? it crashes all the time.
Thank you a ton for this thorough explanation!
No problem :)
great
Thanks! :)
it works , thanks a lot
No problem :)
thanks, you save my day :D
I’m glad I could help! :)
thank you
You’re a good man for explaining out the code so thoroughly. Thanks
Thank you Chris
good
thank u!
Thanks man, u save me 200days of confusion
I never thought of that. Thanks!
Very helpful thank you
THANK YOU SO SO MUTCH!!!
Thank you bro, that really help. Just like @Roger Lopez never though of merge everything in one sentence.
Thanks! Very good.
This worked very well. Excellent
Thank you…
it worked
Great explanation, very helpful. Cheers.
Thanks bro , good answer ! :-)
useful, thanks
Not working for me :( $name = “Anton”; echo $name; $count = strlen($name); echo “
Name-length is: $count
“; //echo substr($name, 0, $count); $random = substr($name, rand(0, $count - 1),1); print “$random
“;You saved the day, I was about to give up, Thanks!
But in this case we have first character, not the random one. Right?
Thanks.