Math II


#1

Hey!

So, I was having trouble for a ridiculous amount of time, and for no reason.
I was right the entire time.

What happened to me was I accidentally deleted a p element between the two separate PHP groupings. So, my code for:

print rand()

and

$name = "yourname";
print substr($name, rand(min,max), 1);

was coming out in the browser right next to each other, making codecademy claim that I wasn't outputting a single character, when I was.

You might want to hit "reset code" and try one more time.

Just in case anyone is as dumb as me lol.


#2

Try this one, it works:



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



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



#3

This is what I tried--it works, but the exercise says it's returning more than one random character--even though it is only returning 1 character:



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

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

Why is Codecademy complaining?
Bill


#4

Thanks alot!! :sweat_smile:


#5

I suppose it's 'cause if you don't use paragraph tags, you output becomes

 rand(1, 20) + " " + $partial

Compiler treats it like " " is printed 'cause of $partial.

so your code has to look like this

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

#6

No idea if this still needed, but I thought I would add my two-sense for fun.

<html>
    <p>
    <?php
    // Use rand() to print a random number to the screen
    print rand();
    ?>
    </p>
    <p>
    <?php
    $name = "matthew";
    $partial = substr($name, 1,1);
    print $partial;
    ?>
    </p>
    <p>
    <?php
    // Use your knowledge of strlen(), substr(), and rand() to
    // print a random character from your name to the screen.
    print rand(1,20);
    ?>
    </p>
</html>