What does `+ ','` mean?

What is the meaning of + ‘,’

19 Likes

Hi there! Can you please explain the role of + ‘,’ after every step in making sandwich?

4 Likes

That is the concatenation operator for joining strings and variables to form a single string. This is the old form of string interpolation.

18 Likes

what is the meaning of +’,’ . Also the repetition of “topping1 and topping2” can not be repeated thus the function can not call duplicated layers; why?

1 Like

This is meant to be an extremely simple example of the usefulness of functions in coding. Without the
+ ',' the return value of sandwich would be breadhamcheesebread instead of bread,ham,cheese,bread. It would be even more readable if we added a space in addition to the comma by changing it to + ', '.
The return value of sandwich would then be bread, ham, cheese, bread.

This function can be customized in any way the programmer wishes. If we wanted to repeat the layers to make a double-decker sandwich (i.e. “bread,ham,cheese,ham,cheese,bread”) we could re-write it like this:

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

The “Learn How to Code” course is a beginning course. There are many ways this example function could be refactored to be more concise and more useful. As you progress through learning to code you’ll see what I mean. Happy coding!

33 Likes

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

// Call the function and store the returned value in ‘result’
result = makeSandwich(‘jelly’, ‘cheese’, ‘ham’)

I can’t solve the problem, what I did wrong ?

It could be just a matter of missing white space after the comma so the toppings are separated as in a sentence.

1 Like

I think it’s just a comma, so it doesn’t read it as breadhamcheesebread, for example. Instead it reads it as bread, ham, cheese, bread. I don’t think it really matters because you’re not printing the strings anyways.

1 Like

I just have one simple question, so why is the first line sandwich= ‘bread’ + ','
but at the end it it’s sandwich += 'bread’

Why use = at the start and use += at the end.

2 Likes

The whole process is that of building a single string from all the aggregate components.

We start with the top slice, so it is a direct assignment…

sandwich = 'bread, '

It was/is unnecessary to concatenate with another literal character so above we have it included in the initial string value.

Given that we want to continue this string (as in grow it) we use the augmentation operator to preserve whatever value to we add to.

sandwich += 'ham, '
sandwich += 'cheese, '

The final augmentation will be the other slice of bread.

sandwich += 'bread'

Notice how we only use direct assignment when we are initializing the string? That should answer your question as to why. Assignment replaces whereas augmentation, well, augments. It adds to or continues.

5 Likes

function makeSandwich(topping1, topping2,topping3) {
sandwich = ‘bread’ + ‘,’
sandwich += ‘peanut butter’ + ‘,’
sandwich += ‘jelly’ + ‘,’
sandwish += ‘pickles’ + ‘,’
sandwich += ‘bread’ + ‘,’
return sandwich
}

result = makeSandwich('peanut butter ', ‘jelly’, ‘pickles’)
where is the fault plz help me !!

Hello, @zinoubghbenz78810214.

Welcome to the forums.

Look closely at your spelling of sandwich on every line. :wink:

Also, you should be using topping1, topping2 & topping3 in your code in place of peanut butter, jelly & pickles
For example:

sandwich += topping1 + ','

thank u very much i see now the difference

1 Like

Very helpful answer. Thanks!

1 Like

We can also view the change in the string due to the value of result because of the commas, commas with space and even no commas

Run the original code along with this additional line at the bottom

window.alert(result)

The three possible cases could be -

  1. With Commas -
Click to view code

// 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’, ‘pickles’)

window.alert(result)

The above code will show the message box as -

bread,burger patty,pickles,bread

  1. With Commas and space
Click to view code

// 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’, ‘pickles’)

window.alert(result)

The above code will show the message box as -

bread, burger patty, pickles, bread

  1. Without Commas
Click to view code

// 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’, ‘pickles’)

window.alert(result)

The above code will show the message box as -

breadburger pattypicklesbread

You can see that in the second and third cases, the burger would not be prepared because we altered the way the result string is represented.