Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

rohit Nandan
rohit Nandan
6,901 Points

Call the function by giving an age in the space for a parameter.? whats that mean >?

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

whats that mean ??

script.js
function random_age(age) {
  Math.floor(Math.random(age) * 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>

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Let's take another look at this code with some comments to describe what's happening.

function random_age(age) {  //start defining function
  Math.floor(Math.random() * age);  //function generates a random number between 0 and the age we send in
}
random_age(30);  //call the function and send it the age 30

A function is simply a piece of code that we run over and over instead of having to write the code 10 times and run it 10 times, we can write it once and "call" it 10 times. Here our function is named random_age. And when we call it, we send it a number. Here, I'm sending in the number 30. It will take that 30 and run whatever code it needs to run with that 30 assigned to the new variable age.

So to call a function we just say the function name and put a pair of parentheses and semicolon after. Any information we need to send into the function will be inside the parentheses.

Hope this helps! :sparkles:

Hi Jennifer

I still do not understand what that number 30 supposed to represent. Is the 30 representing an age? Or just a number we later use to refer to this function?

Thank you!

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Nicolas Van Bylen the 30 is the integer for the age we're sending into the function. When we call the function random_age(30) we're sending that 30 into it as an integer. In the declaration of the function you see the parameter list inside the parentheses here: function random_age(age). You can think of the age part inside the parentheses as a temporary variable declaration. Inside the function age will be now equal to the number 30 (which we sent in when we called /executed/invoked the function). When the function ends that variable age will cease to exist and we'll no longer have access to it.

That makes sense! Thanks a lot!!