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

Functions, Part I Forum

View Course » View Exercise

747 points
5518e67595e37806a6005006_446909977
Submitted by
Sajal Arora
over 2 years ago

5/8 Need a correct code

I am not able to pass through this exercise. Please provideme the correct code for this.


6 votes

permalink

I spent much time on this excercise, though it doesn't seem to be tough, but for some reason it put me away all the time, one of my mistakes was that i deleted comments and paragraphs from the very beginning, then i wanted to put everything in one line, but i guess the result needed middle steps, so i just want to put the whole code here which worked for me for those who are in trouble solving it! Good luck!

<html>
<p>
<?php
// Use rand() to print a random number to the screen
print rand(0,10);
?>
</p>
<p>
<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name='amerikashka';
$randomPosition=rand(0, strlen($name)-1);
$randomLetter=substr($name,$randomPosition,1);
print $randomLetter;
?>
</p>

955 points
54adb54151b8873436005983_744603912
Submitted by
amerikashka
over 2 years ago

4 Comments

0ec715c41123d1f08e1e12376756e7c8?s=140&d=retro Pankaj Shandilya over 2 years ago

Thank's amerikashka it worked for me.

E69f934aaeff37b36c38202f597fe01f?s=140&d=retro Lucky Singh over 2 years ago

thnx...

7c8e6b268c261b3eff823fb383b5c276?s=140&d=retro sai525 about 2 years ago

jahapana thuji great

C137547e697fccca4783eef6a007499c?s=140&d=retro Venkata Rajesh about 2 years ago

oh....finally got the correct answer for this topic. Thanks Amerikashka


5 votes

permalink

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


<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "Alexander";
print substr($name, rand(0, strlen($name) - 1), 1);
?>

This code worked for me

1301 points
5447c1a452f8633db100355f_666308901
Submitted by
Mike
over 2 years ago

1 Comment

54e8fde776b8fe0816002028_323959593 Wicdz over 2 years ago

also worked:
$random = rand(0, strlen($name) - 1);
print $name[$random];


2 votes

permalink

I have done it slightly different. I always worry when my code is different to everyone else's. haha

$name = "Matthew";
$length = strlen($name);
$random = rand(0, $length -1);
print substr($name, $random, 1);

926 points
56b22e96e5f70330610004ef_749985521
Submitted by
Matthew Cottham
over 2 years ago

1 Comment

5573757ee39efe26fa0004dd_985215732 Tahsin Tabassum over 2 years ago

yes it worked for me too!
thanks alot ^_^!


1 vote

permalink

Try refreshing your entire page/code. The code looks fine to me.

<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "Matthew";
$length = strlen($name);
$random = rand(0, $length -1);
print substr($name, $random, 1);
?>

926 points
56b22e96e5f70330610004ef_749985521
Submitted by
Matthew Cottham
over 2 years ago


0 votes

permalink

This code worked for me :)


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


<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "Ingo";
$nameLange = strlen($name);
$nameNum = rand(0,$nameLange);
$nameNum2 = $nameNum-1;
print substr($name,$nameNum2,$nameNum);

?>
</p>

440 points
539ffafa52f863d0a000104e_700417430
Submitted by
Ingo Dörgeloh
over 2 years ago

3 Comments

5518e67595e37806a6005006_446909977 Sajal Arora over 2 years ago

Well, Thanks a lot friend, it worked for me. But why did you put -1?

539ffafa52f863d0a000104e_700417430 Ingo Dörgeloh over 2 years ago

becose we just want to print one letter :)

53fb0e0c548c35173000990f_828020756 Necoti over 2 years ago

Hello guys, We use "-1" because, (lets say our word has 5 letters) "strlen" mesured your word as 5 letters (it goes like 1,2,3,4,5). In "substr" it starts to count your 5 letters starting from "0" (it goes like 0,1,2,3,4). As you see it end at 4 but it is 5 digit in total. So we use "-1" to exact match beween strlen lenght and substr definition.


0 votes

permalink

Nope! I tried refrshing and erasing it with this code:<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "Matthew";
$length = strlen($name);
$random = rand(0, $length -1);
print substr($name, $random, 1);
?>

769 points
5511feb476b8fe1952000771_993560873
Submitted by
Cameron Potter
over 2 years ago