You need to create a function that returns "Hello Kasun!" if the parameter that had been passed was "Kasun". If no parameters were passed, it should return "Hello World!". If the parameters that were passed were invalid, it should throw an error.
There are several methods you can use:
- You can use backticks (`) to create template literals to simplify string concatenation.
- You can use
Array.prototype.join() to make it a bit more complex.
There's a design principle called KISS: Keep It Simple, Stupid.
KISS: Keep It Simple, Stupid
Designs and/or systems should be as simple as possible. Wherever possible, complexity should be avoided in a system—as simplicity guarantees the greatest levels of user acceptance and interaction.
Example:
function getHello(name) {
if (name === undefined) {
return "Hello World!";
} else if (typeof name === "string") {
return `Hello ${name}!`;
} else {
throw new Error("Invalid name.");
}
}
You need to create a function that returns
"Hello Kasun!"if the parameter that had been passed was"Kasun". If no parameters were passed, it should return "Hello World!". If the parameters that were passed were invalid, it should throw an error.There are several methods you can use:
Array.prototype.join()to make it a bit more complex.There's a design principle called KISS: Keep It Simple, Stupid.
Example: