FAQ: Functions - Putting It All Together

What is the error? Are you sure your code is exactly the same? If you could post your code, I’d be happy to try to help. It is possible that the problem is with your browser or device. You can check out this Troubleshooting Guide if you’re certain that your code is correct. The guide explains which browsers and devices work best with Codecademy, and has instructions for making sure everything is up to date.

I hope my weighing in didn’t step on any toes or anything. I just wanted to help someone out.

No worries. There have been times where a temporary glitch with the site is resolved after a few hours, so that may well have been what happened in your case. Seemed to run across just such an instance myself last night in the Python 3 course.

Guys i’m lost i can’t decide wich path i should take
i need a soulotion please !!
TY

Hey @djblack8238907799, welcome to the forums!

We can’t just give you the solution for the exercise. How about you post your code, and we’ll try to help your figure out whats wrong with it?

Hello, how can I make two sandwiches? I have tried with result ‘+’ and ‘,’ but it doesn’t work…

// Define the ‘makeSandwich()’ function
function makeSandwich(topping1,topping2) {
sandwich=‘bread’+’,’
sandwich += topping1 + ‘,’
sandwich += topping2 + ‘,’
sandwich += ‘bread’
return sandwich
}

// Call the function and store the returned value in ‘result’
result = makeSandwich(‘burger patty’, ‘jelly’)
result = makeSandwich(‘jelly’, ‘peanut butter’)

It said that the function is provided in main.js. What is main.js?

main.js is the name of the file where our script is saved. It will be the open tab (or one of them) in the LE.

main_js

1 Like

Hey I’m totally new to this and have one question.
If i change ‘burger patty’ to ‘peanut butter’ and ‘pickles’ to ‘jelly’ in the original function, it works and the sandwich is prepared.
But if i do the whole thing by myself (delete everything and start from scratch), it doesn’t work and always says "Error in code! Expected variable ‘result’ to be defined.
I’ve ensured that everything is correct and done it several times. Is this exercise not ment to be made by oneself and should I just stick with replacing the ingredients in the end.
Help would be appreciated. :slight_smile:

If a math exercise gives us two numbers and expects us to add them, does it count if we change one or both numbers and give the correct result to their addition? Not really. We need to answer the question using the given information.

thanks for your reply

1 Like

What i can’t understand is in the last part of coding;

result = makeSandwich(‘burger patty’, ‘pickles’)

And so this displays the sandwich in the app, However why does it only need the toppings in the ()?
It seems to make no reference to anything in initial function code at the top.
function makeSandwich(topping1, topping2) {
sandwich = ‘bread’ + ‘,’
sandwich += topping1 + ‘,’
sandwich += topping2 + ‘,’
sandwich += ‘bread’
return sandwich
}

Hi, rryannn,
I think your question is about the same as mine. I pasted the answer I got :slight_smile:

" in the original definition, how does the computer recognise the values of topping1 and topping2 as ‘burger patty’ and ‘pickles’ ? Why not anything else ?"

Answer: for any given function(parameter), the parameter is something that can be input by the user or by another action (like a function that has the role to change that parameter). The computer only recognizes these particular parameters as strings.

So for this example, it a further version of the program it would be possible for there to be 2 other functions getTopping1(topping) and getTopping2(topping) that pick out the variable topping from a list of of toppings.

In this immediate program, the final result is that the variable result has been given a value of the function makeSandwich(). It is a result that can be unique for every customer order (in a bigger theoretical implementation).

How does it know that topping 1 is not cabbage or onions? it doesn’t say exactly what topping are anywhere except in the instructions.

I still don’t understand where it says what topping is what. how come jelly is not topping1?

Hello, @byte5723385929.

Long ago, I added some things to the code in the lesson. My code looks like this:

// Define the 'makeSandwich()' function
function makeSandwich(topping1, topping2, topping3, topping4) {
  sandwich = 'bread' + ','
  sandwich += topping1 + ','
  sandwich += topping2 + ','
  sandwich += topping3 + ','
  sandwich += topping4 + ','
  sandwich += 'bread'
  return sandwich
}

// Call the function and store the returned value in 'result'
result = makeSandwich('peanut butter', 'jelly', 'burger patty', 'pickles')

Notice the position (the order in which they appear) of each topping in the line:

result = makeSandwich('peanut butter', 'jelly', 'burger patty', 'pickles')

Now compare that order to this line:

function makeSandwich(topping1, topping2, topping3, topping4) {

The first line calls the function named makeSandwich with 4 arguments. Calling the function just means for its code to be executed. The 4 arguments are assigned as the values of the 4 parameters, so topping1 = 'peanut butter', topping2 = 'jelly', topping3 = 'burger patty' and topping4 = 'pickles'. The position of the arguments inside the parenthesis of the function call determines which parameter they are assigned to.

1 Like

I can see how the toppings and their names follows the same order in the result line… Is that how it’s assigned? I tried moving the topping order around and it still worked just in a different order. How does it know what the topping is and how does it know it’s only one of those 4 items? If i write in onions or cheese it says invalid…so how does it know that topping is not valid?

// Define the 'makeSandwich()' function
function makeSandwich(topping1, topping2, topping3, topping4) {
  sandwich = 'bread' + ','
  sandwich += topping1 + ','
  sandwich += topping2 + ','
  sandwich += topping3 + ','
  sandwich += topping4 + ','
  sandwich += 'bread'
  return sandwich
}

// Call the function and store the returned value in 'result'
result = makeSandwich('jelly', 'peanut butter',  'burger patty', 'pickles')

The order you put the arguments in determines which parameter they are assigned to. Changing the order of the arguments as you have assigns ‘jelly’ to topping1, and so forth. This sample was presumably written to show the relationship between arguments and parameters along with the basic usefulness of functions. I don’t know how you tried to add ‘cheese’ or ‘onions’. If you’d like to show me, I’ll offer an explanation as to why they are not valid. If you try to add more arguments than the number of parameters accepted by the function, that’s a problem. It could also just be that whoever wrote this app didn’t include ‘onions’ or ‘cheese’ in their code since they’d also have to have a graphical representation of each for the sandwich built in the browser. Note: I just had a look at the code, ‘cheese’ is valid, but ‘onions’ are not.

I’m not sure what parameters and arguments are, i know they were covered i just don’t get it. I just replaced one thing with the other in the topping list, instead of jelly i tried cheese then onion. If it’s not valid that’s ok but where does it check to see that onions are not valid? There seems to be a secret list of things that are ok and things that not but where is that listed except for in the last part with the specific sandwhich?

A function is simply a block of reusable code. I know you are somewhat familiar with JavaScript, so if I wrote a function that did nothing more than say hello to a person using their first name, my function would need a parameter. That parameter would be the name to say hello to. A parameter is really just a variable that has its value assigned when the function is called. That variable (parameter) can be used inside the function. The function could then return a value, or just carry out some task or tasks. The line of code that calls the function uses the name of the function followed by (). If the function takes a parameter or parameters, then we need to provide values to the function when we call it. Those values are called arguments, and we provide them by placing them inside the (). So, my function to say hello could look like this:

function sayHello(firstName) {
   console.log('Hello, ' + firstName);
};

Then if I want to actually call the function, I would add the code to do so:

function sayHello(firstName) { //firstName is the parameter
   console.log('Hello, ' + firstName);
};

sayHello('Byte5723385929'); //'Byte5723385929' is the argument

Output:

Hello, Byte5723385929’

The function can be reused. I could call it repeatedly to say hello to several people:

function sayHello(firstName) { //firstName is the parameter
   console.log('Hello, ' + firstName);
};

sayHello('Byte5723385929'); //'Byte5723385929' is the argument
sayHello('Tod'); //'Tod' is the argument this time
sayHello('Roy'); //now 'Roy' is the argument
sayHello('Sam'); //and now 'Sam' is the argument

Output:

Hello, Byte5723385929
Hello, Tod
Hello, Roy
Hello, Sam

Using other techniques, this code could be further optimized, but that will come later.

Does this help at all?

It’s not secret, it’s just not immediately evident since it doesn’t pertain to the lesson. It is in the behind the scenes code that runs when you click the Run button. That code takes what you’ve written in the editor, evaluates it, and produces the graphical representation of a sandwich in the browser based on what your code says.

2 Likes