This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Benjamín Romero
almost 11 years

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

60 votes

Permalink

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>
points
Submitted by Chris McKay
almost 11 years

35 comments

Benjamín Romero almost 11 years

That’s great. It worked!! :) Thanks

courseRockstar68051 almost 11 years

Great answer with a great explanation. Thanks.

Chris McKay almost 11 years

No problem, Glad I could help!

Taito Llama almost 11 years

Fantastic explanation, thanks!

Chris McKay almost 11 years

No problem! :)

Michiel Welling almost 11 years

thanks mate, this was very helpful. btw, is it just me or does anyone have problems with this site? it crashes all the time.

Stacey almost 11 years

Thank you a ton for this thorough explanation!

Chris McKay almost 11 years

No problem :)

Vikram Nithyanandam over 10 years

great

Chris McKay over 10 years

Thanks! :)

Mircea Rojnita over 10 years

it works , thanks a lot

Chris McKay over 10 years

No problem :)

optimizer over 10 years

thanks, you save my day :D

Chris McKay over 10 years

I’m glad I could help! :)

Vishal Shira over 10 years

thank you

matt raney over 10 years

You’re a good man for explaining out the code so thoroughly. Thanks

AjayMartin over 10 years

Thank you Chris

nagendrababu865 over 10 years

good

nagendrababu865 over 10 years

thank u!

Ekom Otu over 10 years

Thanks man, u save me 200days of confusion

Roger Lopez over 10 years

I never thought of that. Thanks!

Codeicidal over 10 years

Very helpful thank you

tutunja over 10 years

THANK YOU SO SO MUTCH!!!

Carlos E. Guillen over 10 years

Thank you bro, that really help. Just like @Roger Lopez never though of merge everything in one sentence.

Fernanda Lima over 10 years

Thanks! Very good.

Robyn-Dale Samuda over 10 years

This worked very well. Excellent

Paramanathan ilangeeran about 10 years

Thank you…

deekiarie about 10 years

it worked

CM13 about 10 years

Great explanation, very helpful. Cheers.

Pranit almost 10 years

Thanks bro , good answer ! :-)

useful, thanks

Anton Boksha over 9 years

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

“;

Paul Rosenbeck over 9 years

You saved the day, I was about to give up, Thanks!

Day Will Come almost 9 years

But in this case we have first character, not the random one. Right?

Bryan almost 9 years

Thanks.

Answer 515c9c0e71279fdbc30000bc

8 votes

Permalink

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>
points
Submitted by Josiah
almost 11 years

1 comments

Petsky over 10 years

Hi Josiah, rand() should have 2 parameters, maybe you lack comma after strlen($myName) =)

Answer 515b1173e8a240ceb50023af

4 votes

Permalink

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:

points
Submitted by bruno celaya
almost 11 years

2 comments

Benjamín Romero almost 11 years

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

3 votes

Permalink

This code works:

points
Submitted by Bine Intentionat
over 10 years

Answer 515be98272b62d5148000149

1 vote

Permalink

points
Submitted by AdrianoC
almost 11 years

2 comments

Benjamín Romero almost 11 years

It’s my same problem and it everything fine apparently

bruno celaya almost 11 years

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

1 vote

Permalink

Thanks for the explanation, was very helpful, but i have a small misunderstanding. I writte my code like this:

points
Submitted by Popa Viorel
almost 11 years

Answer 5262ea1df10c60822300b51d

1 vote

Permalink

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>
points
Submitted by Luca
over 10 years

1 comments

Manaz Ahmed almost 9 years

its working..

Answer 5279d82aabf8210e18000ac9

1 vote

Permalink

points
Submitted by Anthony Bryant
over 10 years

Answer 5332eafb548c357886004eaa

1 vote

Permalink

points
Submitted by Aumkar Thakur
about 10 years

Answer 515c86f91d70a483ba0005d7

0 votes

Permalink

thanks for the help, I was kinda close, yet so far away :)

points
Submitted by Jeff Shamley
almost 11 years

6 comments

Kamalrajsinh Sodha over 10 years

still the problem is same as above after refreshing also

Luca over 10 years

use this: print ($name[rand(0, strlen($name-1))]);

Kamalrajsinh Sodha over 10 years

The output on the console is true but then also it displays the error.

Luca over 10 years

Write your code down so I can check it; do it in a post and not in a comment please

Kamalrajsinh Sodha over 10 years

Check the post, I have posted it

Luca over 10 years

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

0 votes

Permalink

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

points
Submitted by bruno celaya
almost 11 years

1 comments

Benjamín Romero almost 11 years

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

0 votes

Permalink

points
Submitted by Sohil Sharma
about 10 years

Answer 53daa906631fe94a3800043e

0 votes

Permalink

I have the following code and it does not work on the editor

points
Submitted by rockysreenivasa
over 9 years

Answer 54420348282ae3b07e0010c2

0 votes

Permalink

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.

points
Submitted by biggig
over 9 years

1 comments

tenzinchoenyi over 9 years

thats the correct answer just try this’

Answer 5463239752f8638637001942

0 votes

Permalink

points
Submitted by c0rpse
over 9 years

Answer 546ee3f57c82ca31300014e5

0 votes

Permalink

points
Submitted by tenzinchoenyi
over 9 years

1 comments

tenzinchoenyi over 9 years

this is correct answer for printing the random character

Answer 54ce7a0195e3787208003819

0 votes

Permalink

points
about 9 years

Answer 554fb0d6d3292f26c00001a1

0 votes

Permalink

Simply do :-

$name = “Pravvy”; $len = strlen($name); $rand = rand(0, $len - 1); echo $name[$rand];

points
Submitted by Pravash Panigrahi
almost 9 years