Vaguely about how to approach this (write a function, returning a string)

Therefore, I need to write the plusLettuce function, which takes one parameter as an argument, and the function should return a string with the argument and the phrase "plus salad". So I guess if I type plusLettuce ("Onion"); in my console I should get "Onion plus Salad" as my output.

This is what I have so far .. so I wrote my function with a parameter, and I'm confused what to do next. (I'm sorry noon) Can I make a variable word? I just got stuck on what my next step should be. Please help.

var plusLettuce = function(word) {
   var word = 
}
+4
source share
4 answers

+ , return, :

var plusLettuce = function(word) {
   return word + " plus lettuce";
};
plusLettuce("Onions"); // "Onions plus lettuce"
+6

JS + .
word ( ), var word.

So

function plusLettuce (phrase) {
  // I don't say `var phrase`, because it already exists
  var phrasePlusLettuce = phrase + " plus lettuce"; // note the space at the start
  return phrasePlusLettuce;
}
+2

, . , .

var plusLettuce = function(word) { // I take the var word from here...
   return word + ' plus lettuce'; // ...and then use it here.
};
console.log(plusLettuce('Onions')); // This is where I assign the var word.

, , , plusLettuce , " ". console.log();

+2

, , , - .

function plusLettuce (phrase){
  var staticWord = ' plus lettuce';
  return phrase + staticWord
}
console.log(plusLettuce('Onions'))

Remember that the parameter / argument is a variable accessible only by the function, the static part, meaning that it will always be the same, can be the purpose of the variable to save the code. and the dynamic part, which is the parameter, will differ each time in accordance with what is passed to the function called each time.

0
source

Source: https://habr.com/ru/post/1615842/


All Articles