Got this error when running my code for this challenge.
Your function should return the string 'Hello, World!' . Expected: Hello, World! but your function returned: 'Hello, World!' . Our test invoked your function and saved the value it returned. We expected it to equal the exact value specified ( 'Hello, World!' ) and found it did not.
Not sure if you’re still stuck on this one, but I hit the same error just now.
After looking at the solution they provided apparently the real solution requested is Hello, World! and not ‘Hello, World!’, as in the code accepted my answer as correct once I removed the apostrophes around the string.
In the “Helpful Notes:” section of this exercise, it explains that our code for this exercise can be either a function expression or a function declaration. What is the difference?
I looked up on Mozilla Development Network’s MDN documentation, but the answer is still unclear and also introduced another term–function statement. So my question is, what is i the different between function declaration, function expression, and function statement?
Actually, both are anonymous function expressions.
Consider the example:
// Function Declaration
function func1() {
console.log("Hello");
}
// Anonymous Function Expression (arrow syntax)
const func2 = () => {
console.log("Hello");
};
// Anonymous Function Expression
const func3 = function() {
console.log("Hello");
}
// Named Function Expression
const func4 = function greet() {
console.log("Hello");
}
Only func1 is a function declaration. The rest are function expressions.
// Declarations and expressions
...
// Function Calls (All of them work)
func1(); // Output: Hello
func2(); // Output: Hello
func3(); // Output: Hello
func4(); // Output: Hello
Contrast this with the function calls being placed before the declarations and expressions.
// Function Calls (Only the Function Declaration version works)
func1(); // Output: Hello
func2(); // Error - function not defined
func3(); // Error - function not defined
func4(); // Error - function not defined
// Declarations and expressions
...
That has to with hoisting. Function declarations are hoisted, so they can be called even if the calls are placed before the declaration. Function expressions aren’t hoisted.