Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused on how to approach this (writing a function, returning a string)

So I have to write a function plusLettuce that accepts one parameter as an argument and the function has to return a string that has my argument and the phrase "plus lettuce". So I'm guessing if I type in plusLettuce("Onions"); into my console I should get "Onions plus lettuce" 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 a total noon sorry) Do I make a variable word? I'm just stuck on what my next step has to be. Please help.

var plusLettuce = function(word) {
   var word = 
}
like image 940
user5539793 Avatar asked Dec 25 '22 12:12

user5539793


2 Answers

You can use the addition operator + to concatenate strings, and the return statement to return the result of the function call:

var plusLettuce = function(word) {
   return word + " plus lettuce";
};
plusLettuce("Onions"); // "Onions plus lettuce"
like image 186
Oriol Avatar answered Dec 27 '22 02:12

Oriol


JS uses + for string concatenation.
You're also overwriting your word (which is already there, in your function), when you declare a new 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;
}
like image 40
Norguard Avatar answered Dec 27 '22 02:12

Norguard