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

Jodia Steenkamp
Jodia Steenkamp
Front End Web Development Techdegree Student 1,378 Points

How do you "Call the function by giving an age in the space for a parameter."?

I added the parameter within the parenthesis but I'm sure what is meant by "Calling the function by giving an age in the space for a parameter."

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

5 Answers

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there! Your very bottom line is the call to the function. But you need to tell it what age to send! You could do something like this:

random_age(90);

Hope this helps! :sparkles:

Let's say we had this function:

function sayHelloTo(person) {
  console.log("Hello, " + person + "! Nice to meet you!");
}

And I called the function like this:

sayHelloTo();

It would cause an error. This is because our sayHelloTo function was expecting a parameter, which in this case is person. So, to make this work, let's pass in a parameter (or, another way to say it is "Calling the function by giving an age in the space for a parameter").

sayHelloTo("Jodia");

This is now fixed. The output of this is:

Hello, Jodia! Nice to meet you!

So, now do you get a clue of what the challenge meant about "Calling the function by giving an age in the space for a parameter"? :smile:

Good luck, Alex

Oh man, it took a while to type out my answer that's why it took so long (and while I was typing Jennifer Nordell answered :smile:). I still like your answer Jennifer Nordell so I'll give you a upvote :)

~Alex

Jodia Steenkamp
Jodia Steenkamp
Front End Web Development Techdegree Student 1,378 Points

Thanks :) I eventually tried and tried until I got it right (urgh!)

What is the reason for the 'call to function' being out of the relevant function? Should it not be within the curly brackets?

Jennifer Nordell
Jennifer Nordell
Treehouse Staff

No, it shouldn't. The code inside the curly braces is the code we want to run every time we call/invoke/execute the function. That call (last line) there tells the browser "Ok now run/execute this code I've defined.." And the thing in the parentheses is the information we're passing to that code. Hope that makes sense!

I like that simple answer :)

~Alex

You might find this Treehouse video helpful about functions. :smile:

Hope it helps! ~Alex

Jodia Steenkamp
Jodia Steenkamp
Front End Web Development Techdegree Student 1,378 Points

Great stuff, thanks Jen and Alex for the help. I understand now :) Whether that intel remains, I'm still to find out. Cheers :)

:) when first learning programming, function are the most confusing thing ever. LOL but I'm suprised how fast you got the hang of it (when I was learning functions it took a couple days to understand how it works because at that time I wasn't using Treehouse I was reading books about programming, and there was no community for books, of course so I can't ask questions except for asking my father , who also knows programming)