155k views
0 votes
Create a function greeting(name) that accepts a name argument and returns a personalized greeting if a name is present (Must be done in Javascript)

Example:

INPUT: greeting("Charlotte");

OUTPUT: "Hello, Charlotte!";


INPUT: greeting();

OUTPUT: "Hello!";

1 Answer

2 votes

Answer:

Following are the program in the JavaScript Language.

//define function

function greeting(name)

{

//set string type object

put = "Hello"

//set the if conditional statement

if (name) {

//concatenate with the object

put += ", "+name;

}

//concatenate "!" with object

put += "!";

console.log(put);

}

console.log(new greeting("Charlotte"));

Output:

Hello, Charlotte!

Step-by-step explanation:

In the following program, Firstly we set a function "greeting" and pass an argument "name", inside the function.

  • Set a string type object "put" and initialize to "Hello" in it.
  • Set the if conditional statement to check the the argument "name" and concatenate the value of the "name" before "Hello".
  • Then, concatenate "!" after the "Hello" in name and print the object "put".

Finally, we call the function "greeting" through the pre-defined function "console.log()" by passing the sting inside the parameter.

User Robin Wang
by
7.1k points