Functions sheet 5

Hi, there ! :slight_smile:

I’m starting from scractch after 3 years of absence and there’s a few things I wonder : in the original definition, how does the computer recognise the values of topping1 and topping2 as ‘burger patty’ and ‘pickles’ ? Why not anything else ?
And in the exercise, how come ‘peanut butter’ and ‘jelly’ are recognised as topping1 and topping2 as well ?
I mean is it just because it’s a beginners course or is there a logic I can’t grasp ?
Thanks for your answer
and stay safe !

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

" 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).

I wouldn’t get too frustrated. If one just completes the courses to get the right answer, then on won’t get as much value from each exercise. The key is for each exercise to reflect on what the core issues at play are, therefore expanding your understanding of such concepts. Anything that unsettles you, you should look up further documentation or ask like you’re doing! There is a learning curve, but it does get less frustrating once you start having a structure of concepts to sustain your doubts and fight back.

Thank you for your answer, it’s very helpful. :slight_smile: