Functions Math II


#1

I dont know what I am doing wrong....

lauhi<html>
    <p>
    <?php
    // Use rand() to print a random number to the screen
$name = "Jack Offsalot";
    ?>
    </p>
    <p>
    <?php
    // Use your knowledge of strlen(), substr(), and rand() to
    // print a random character from your name to the screen.
$n = rand(0, strlen($name) - 12); 
echo substr($name, $n, $n+1);
    ?>
    </p>
</html>

#2

change echo substr($name, $n, 1); because you'll need one character from your name


#3

Same issue here. However, I found multiple ways to print out one character, but it seems that the program doesn't accept the solutions I have. Looks like the program only accepts one kind of solution to me?

Here's what I've done:

    // My name
    $name = "Frank";
    // Picking a random number
    $ran = rand(0, strlen($name) - 1);
    // Determine the random character
    $ranChar = substr($name, $ran, 1);
    // Print out the random character
    echo "<br>The random character is: ";
    print $ranChar;

This prints out:

Just some random number: 1125058485

The random character is: r

Even when I change this: $ran = rand(0, strlen($name) - 1); into this: $ran = rand(0,1); It prints out only 1 character but is not accepted as an correct answer.

So, seems like a legit solution to me?

!!-----------------------------------------UPDATE-----------------------------------------!!

Looks like the program only accepts just the print of a single character, which means it doesn't want to see an echo like in my case. So remove any excess stuff seems to do the trick!

    // My name
    $name = "Frank";
    // Picking a random number
    $ran = rand(0,1);
    // Determine the random character
    $ranChar = substr($name, $ran, 1);
    // Print out the random character
    print $ranChar;

#4

The problem is solved in detail here.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.