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 JavaScript Arrays Loop Through Arrays Loop Through an Array

argument

What connects the loop to the playList how does the function know to look at the array playlist, i dont have tech speak

12 Answers

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

Hi there, jasonj7! I've seen your response to our other thread from a while ago just now, too. I think you're getting confused about some of the terminologies. Notably, "argument" and "parameter". I actually wrote a blog about this a while ago. It's a bit wordy, but it's about a 6-minute read. I feel like if you read it through to its conclusion, you will have a better understanding of why you would even want a function, what an argument is, and what a parameter is.

Here's hoping this blog article on functions helps! :sparkles:

Thanks I will print it out and read thoroughly. That is a very good read, so parameters are a bit like variables (I'm calling them parameter variables now), and i guess i can import arguments into functions?

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

jasonj7 That's exactly it. They are variables. They are variables that have no value whatsoever. They are just waiting for values to be given to them. We give them when we call the function.

function sayHi(name) {  // name doesn't have any value before we call it
    console.log(`Hi there, ${name}!`);
}

// prints out "Hi there, undefined!" ... wooops
sayHi();  // forget to give the name variable a value

// prints out Hi there, Jason!
sayHi("Jason");  // name variable now gets a value of  "Jason"

I wonder if you could look at my code. https://w.trhou.se/mawudohnw6

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

Hi there, jasonj7! I tried looking at the snapshot but it seems like it may have been deleted. I get a bummer saying that the snapshot doesn't exist :smiley:

I was trying to play around with parameters and arguments and gain more knowledge. All criticism welcome. :-)

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

Hi there, jasonj7! The problem here is that you have the parameters, but then you don't use them*. Parameters are variables but they are only available inside that function. I took the liberty of altering your responsejs.js file and think you might be interested in the results.

Feel free to comment out one of the console.log() at the bottom, but if you leave them both uncommented you will have to answer "yes" both times to the prompt. We're running the function twice, and the prompt occurs every time the function is run.

var jasonSandwich = "Chicken sandwich";
var jasonDrink = "Coke";
var jasonCrisps = "Ready Salted Crisps"

function goToTheShops(sandwich, drink, crisps) {

    var answer = prompt("Is it time to shop");
    if (answer === "yes") {
        // alert("Let's go to the shops");
        return `YAY! Now I have a ${sandwich}, ${drink}, and ${crisps}`;
    } else {
        alert("No, let's stay at home.");
    }
}  

console.log(goToTheShops(jasonSandwich, jasonDrink, jasonCrisps));
console.log(goToTheShops("ham and cheese sandwich", "root beer", "Doritos"));

I'm assuming here that we're both getting lunch :smiley: So I put in my order, too.

Hope this helps! :sparkles:

edited for code clarification

I'm going to make your variable names a little different so that it becomes clear which is a global variable and which is a variable specific to inside the function.

I'm glad I got so far with it, but your way is so much better, you make it look so easy...
I guess I didn't think far enough into the program. Am i right in thinking that the function is reaching outside of itself in regards of scope? Oh my mistake youve changed variable names oh okay :-)

I think my top variables are like a default lunch but now you have added the ability to have anything, this is really good. I guess I'm paying for lunch my treat :-)

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

jasonj7 It could reach outside into the global scope. I like to think of functions like cars with heavily tinted windows. They can see everything outside themselves: people walking dogs, stores, streets, trees. That is the "global" scope. They can even see other cars (functions). But nothing can see inside that car. It could be pristine and clean or have a thousand fast-food wrappers on the floor. No way to tell.

But we're sending the function the values that you declared up at the top. So the first argument which holds the value stored in jasonSandwich is assigned to the first parameter sandwich. The second argument jasonDrink is assigned to the second parameter drink. And the third argument jasonCrisps is assigned to the variable crisps.

If you were to try and add a console.log(crisps); at the bottom of the file, you would get undefined, because crisps only exist while that function is running.

At line 10 return YAY! Now I have a ${sandwich}, ${drink}, and ${crisps};

these are the same as the parameters, should they be changed to variable names?

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

jasonj7 The main point that I'm trying to get across to you is that parameters are variables. They are variables specific to that function.

This would also work:

function goToTheShops(blah, whatever, silly) {

    var answer = prompt("Is it time to shop");
    if (answer === "yes") {
        // alert("Let's go to the shops");
        return `YAY! Now I have a ${blah}, ${whatever}, and ${silly}`;
    } else {
        alert("No, let's stay at home.");
    }
} 

The arguments are the values and the parameters are the variables. So the first parameter (variable) gets the first argument (value). The second parameter (variable) gets the second argument (value).

So if I were to do this:

console.log(goToTheShops("meatball sub", "Sprite", "Cheetos")

It's exactly as if I had said "When you run this function, assign the string "meatball sub" to the variable blah. Then assign the variable whatever the string "Sprite". Then assign the variable silly the string "Cheetos". Run this code in this function using the variable names defined in the parameters and line up these values one to one that are in the call to the function.

Oh my goodness I'm so stuck is it normal for people not to understand this. I feel like I'm using a dart board and if I throw 30 darts I get a result with one of them, but I don't know why... so frustrating. Thanks for helping though.

So here we add our own strings (our own meals) to goToTheShops, and jasonsandwich is more a default set of variables.

So one way can be fixed another way is changable. hmm.

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

Not really. Both methods are changeable. Consider the following.

var jasonSandwich = "Chicken sandwich";
var jasonDrink = "Coke";
var jasonCrisps = "Ready Salted Crisps"

function goToTheShops(sandwich, drink, crisps) {

    var answer = prompt("Is it time to shop");
    if (answer === "yes") {
        // alert("Let's go to the shops");
        return `YAY! Now I have a ${sandwich}, ${drink}, and ${crisps}`;
    } else {
        alert("No, let's stay at home.");
    }
}  

console.log(goToTheShops(jasonSandwich, jasonDrink, jasonCrisps));
console.log(goToTheShops("ham and cheese sandwich", "root beer", "Doritos"));

jasonSandwich = "barbeque";
jasonDrink = "iced tea";

console.log(goToTheShops(jasonSandwich, jasonDrink, jasonCrisps));

It's just that one method is a little more direct than the other. You can send the values in directly or you can send them by referencing them through a variable. But those are variables so you can change them :smiley:

arr so we are updating the arguments which change the order of our food because they are passed to the parameter variables of the function. and parameters are placeholders, without them there is no place for arguments to exist. oh oh oh so we can return parameters back into the function code block, in whatever way we want!!!! oooh oohh ooohh tell me im right hahaha didnt think code would be so exciting

I think i see light at the end of the tunnel!!!

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

jasonj7 Ok I think we have a winner now! That's exactly right!

return `YAY! Now I have a ${sandwich}, ${drink}, and ${crisps}`;

To this:

return `YAY! Now I have a ${crisps}, ${drink}, and ${sandwich}`;

They print out in reverse order than what we had originally. Those are variables. And they were assigned a value the moment we called the function and sent the values to be assigned to them. They are assigned in order, but you can use them in any order you like. They're just... variables. Just like you've been dealing with all this time. The only real difference is that they're variables that belong to that function and nothing else. :smiley:

arr so we are updating the arguments which change the order of our food because they are passed to the parameter variables of the function. and parameters are placeholders, without them there is no place for arguments to exist. oh oh oh so we can return parameters back into the function code block, in whatever way we want!!!! oooh oohh ooohh tell me im right hahaha didnt think code would be so exciting

I think i see light at the end of the tunnel!!!

Was i right in my thinking here....

https://w.trhou.se/vde5jq3cjc I'm hoping to work on this one but I am struggling again.. sorry I get message or mini not defined but they are they are variables.

https://w.trhou.se/b9q4ajuyi2 I see where I have gone wrong, I'm assuming its best practise to keep declared variables at the top of the file?

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

Hi there, jasonj7! There's a few things going on. No, Mini is not defined. I'm pretty sure you meant the string "Mini" :smiley: Also, if you want to print out that message your console.log either needs to be inside the function or you need to return the message. Take a look:

function carsToBuy( car1, car2, car3 ) {
    var car1 = "Mini";
    var car2 = "Ford";
    var car3 = "Peugot";
    var message = (`I really wanted to have a ${car1}, my wife has a ${car2}. I like the ${car3}.`);
  return message;
} 

console.log(carsToBuy("Tesla", "Honda", "FordFusion"));

But there's a problem here. I'm trying to assign the cars I like to it the variables car1, car2, and car3, but your variables inside your function are completely overwriting what I'm sending in. But I'm sure that's not what you're intending.

function carsToBuy(var car1, var car2, var car3)  // this is not syntactically correct, but these ARE variables. I'm trying to make this point abundantly clear

So try this:

function carsToBuy( car1, car2, car3 ) { 
  var message = (`I really wanted to have a ${car1}, my wife has a ${car2}. I like the ${car3}.`);
  return message;
} 

console.log(carsToBuy("Mini", "Ford", "Peugot"));
console.log(carsToBuy("Tesla", "Honda", "FordFusion"));

And you can use those variables in any way you want :smiley:

function carsToBuy( car1, car2, car3 ) { 
  if(car1 == "Mini") {
      console.log("Oh I love the Mini, too!");
  }
  var message = (`I really wanted to have a ${car1}, my wife has a ${car2}. I like the ${car3}.`);
  return message;
} 

console.log(carsToBuy("Mini", "Ford", "Peugot"));
console.log(carsToBuy("Tesla", "Honda", "Ford Fusion"));

Give these a whirl! :sparkles:

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

Woops just saw your updated workspace snapshot! Yes, I tend to keep variables at the top of the file, but that's me :smiley:

Aww thank you, I'm just checking I'm on the right track. :-)

My real-world reference for a Parameter and a Return Value in a Function has always been; Input and Output. You have built a machine, that takes a specific type of Something into it, and sends a specific type of Something Else out again: You could have a function that expects a Number (var), a Word (string) or List (array) as the Input, and will return a Sentence (string) based on the Input got.