JavaScript Treehouse Club - MASH MASH - JavaScript The Structure of Functions - Part 3 of 3

SISLEY NGUYEN
SISLEY NGUYEN
1,835 Points

Call the function by giving an age in the space for a parameter.

Call the function by giving an age in the space for a parameter.

script.js
function random_age(age) { // new function called random_age that taks one parameter)
  Math.floor(Math.random() * age);
}
random_age();
index.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Futuristic MASH</title>
    <link href="normalize.css" rel="stylesheet">
    <link href="style.css" rel="stylesheet">
  </head>
  <body>
    <h1 class="logo"><img src="img/mash-logo.svg" /></h1>
    <p class="instructions">Fill in the blanks and your future will be foretold.</p>
    <form action="" method="post" id="mash">
      <div class="choice-bucket">
        <h4 class="highlight">What's your future pet?</h4>
        <input name="pet[]">
        <input name="pet[]">
        <input name="pet[]">
        <input name="pet[]">
      </div>
      <input type="submit" value="Tell my fortune">
    </form>

    <script src="script.js"></script>

  </body>
</html>

2 Answers

Sean T. Unwin
MOD
Sean T. Unwin
Treehouse Moderator 28,374 Points

On line 4 of script.js insert a number between the brackets of random_age();.

:-)

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 83,234 Points

Hi there,

You're very close. :-)

function random_age(age) { // new function called random_age that taks one parameter)
  Math.floor(Math.random() * age);
}
random_age();

What's happening is you've written a function that contains a parameter called age, that's the thing in between the parentheses. You've written a function which is a piece of organised code that does something. But just writing the function on it's own won't do the job.

To get the function to perform the action you have to write something called a function call.

random_age(6);

This is basically just the function name on it's own. However you can pass values into it. This function takes one parameter which is waiting for a value. So the value in this case is what replaces the age parameter. I used 6 for this example.

Hope this makes sense :)