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

Problem with my parameters and arguments?

https://w.trhou.se/0xc4i4d3xb not sure whats wrong but i know i'm not getting a result i'd hoped for?

Cameron Childres
Cameron Childres
11,818 Points

Hi jasonj7,

Can you give some details on what you're wanting this code to do?

I see that you have a prompt that gives two different alerts, one for an input of "yes" and another for any other input. What are you wanting the "sandwich", "drink", and "crisps" variables to be used for?

hmmm see I'm just not sure, I think I'm trying to make the program follow correctly as a conversation would. I'm really playing with it and trying to understand parameters and arguments that I have been stuck on since I can remember. I was told by treehouse staff to gain experience and although that's what I need I'm not sure if I'm just getting more confused. I keep coding until I get it right, but its like a dartboard after 30 darts I finally hit the right place, but don't understand how.

https://w.trhou.se/hslvhpyeld

So i have tried here to create a function with parameters and arguments to learn from however I am not sure if im coding correctly.

Sorry for the somewhat vague response.

Cameron Childres
Cameron Childres
11,818 Points

That's alright, it's great that you're creating your own code to wrap your head around how parameters and arguments work. Keep it up!

What I'd suggest first is having a defined goal of what you want the code to do -- from your dartboard analogy, come up with the "bullseye" you want to hit, otherwise you'll just be striking out in random directions. The code will do exactly what you tell it to so it's vital to know what you want from it. Mace's response is a great example of this -- they had a clear goal in mind (find out if you're going to the shop, then build an order using parameters and write it to the document) which informed them on how to structure the code to achieve that result.

1 Answer

Hi again Jason,

While I'm not entirely sure what you would like to achieve, I feel like the code below might do what you were hoping for it to do.

The message you're printing to your console, should probably only be printed if you 'go the the shop', so I placed it inside of the if-statement where 'yes' is true (as you can see, I'm printing it to the document in my example, however).

So in the function, you set the parameters to sandwich, drink and crisps. When executing the function (goToTheShops();) you can assign the value to each parameter, like so:

function goToTheShops(sandwich, drink, crisps) {
    var answer = prompt("Is it time to shop");

    if (answer === "yes") {
        alert("Let's go to the shops");
        document.write('Hi, could I have a' + ' ' + sandwich + ' ' + 'sandwich and' + ' ' + crisps + ' ' + 'crisps? Oh, and please add a' + ' ' + drink + ' ' + 'as well!');
        // "Hi, could I have a Chicken sandwich and Ready Salted crisps? Oh, and please add a Coke as well!"
    } else {
        alert("No, let's stay at home.");
    }

} 

goToTheShops("Chicken", "Coke", "Ready Salted"); 

Or, you can use variables instead to get the same result:

function goToTheShops(sandwich, drink, crisps) {
    var answer = prompt("Is it time to shop");

    if (answer === "yes") {
        alert("Let's go to the shops");
        document.write('Hi, could I have a' + ' ' + sandwich + ' ' + 'sandwich and' + ' ' + crisps + ' ' + 'crisps? Oh, and please add a' + ' ' + drink + ' ' + 'as well!');
        // "Hi, could I have a Chicken sandwich and Ready Salted crisps? Oh, and please add a Coke as well!"

    } else {
        alert("No, let's stay at home.");
    }
} 

var sandwich = "Chicken";
var drink = "Coke";
var crisps = "Ready Salted"; 

goToTheShops(sandwich, drink, crisps); 

I hope this is at least close to what you would like to achieve and that you understand what I'm saying.