FAQ: Functions - Putting It All Together

Sorry, here it is: https://www.codecademy.com/paths/code-foundations/tracks/learn-how-to-code/modules/bop-ii/lessons/bop-functions/exercises/all-together

1 Like

The parameter variables should all be unique. Work with the two parameters given, topping1 and topping2.

Would someone be able to tell me what’s the reasoning behind writing the function in multiple steps as…

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

…and not as a single row?

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

Is it just to make it easier to read?

Quite possibly, yes. At this point is difficult to say how comfortable the new learner is with concepts such as concatenation.

ES6 gives us a really special tool for constructing a string from literals and variables… The template literal syntax.

function makeSandwich(topping1, topping2) {
    return `bread, ${topping1}, ${topping2}, bread`
}

This will be covered in the lessons on strings and string concatenation so let this slide for the time.

2 Likes

Hi, can someone help me brake down the instructions in { }, I don’t understand the logic behind it :confused:

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

Technically, there is no logic, only inline assignments. The first line in the function block defines a string object, sandwich and assigns the first word concatenated with a comma string character. The next three lines continue the assignment using an assignment operator that extends the string in one step. += is sometimes referred to as an augmentation operator.

The finished result will be string composed of the assignments in all four lines.

console.log(sandwich)
// bread,ham,cheese,bread

Thank you! Made it clear :slight_smile:

1 Like

3 posts were split to a new topic: What does return do?

Hi, I’m having the same problems as kellebelle99,

https://www.codecademy.com/paths/code-foundations/tracks/learn-how-to-code/modules/bop-ii/lessons/bop-functions/exercises/all-together

I tried to running the function that was already set up, but that doesn’t make the burger sandwich so I don’t know where I messed up.

Could someone explain mtf in a different way?

Hello, @dog_geist! Welcome to the forum.
If you wouldn’t mind copying and pasting the code you are having trouble with in a reply to this post we’ll be happy to try to help. Please click on the </> icon before pasting your code, and then paste it in the space indicated. Without seeing the code in question, it is really difficult to speculate what may be wrong.

Hi, thanks for responding, but interestingly enough it works now.

// 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('peanut butter', 'jelly')

It’s very odd, because I did nothing different. At first it wouldn’t even run the default example.

1 Like

I guess we chalk it up as a glitch. Happy coding!

1 Like

Hi midlinder,

I have the same problem as dog_geist. My code is the same as his, but it doesn’t run. I get an error and can’t move on.

Can you please help me?

If your situation is anything like mine, just wait a couple of hours and get back to it.

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’)