Need help with Intro 5/13, please. "You did not call the greeting function."


#1

I can't figure out what I'm doing wrong on JS Intro to Functions, 5/13. The error I get is: "You did not call the greeting function. Pass it a name!"

Here is my code:

// Nicely written function:
var calculate = function (number) {
var val = number * 10;
console.log(val);
};

// Badly written function with syntax errors!

var greeting = function (name) {
var name = "Andy";
console.log(name);
};


#2

When we write a variable in the formal parameter of a function, it's the same as if we had declared it with var, except the declaration is implicit. Writing,

var greeting =  function (name) {
    var name = "Andy";
};

destroys the parameter variable. Instead, write something along these lines...

var greeting = function (name) {
    console.log(name);
};
greeting("Andy");

#3

Thank you very much! To be clear, was the "var name = "Andy"" the part you mentioned that destroyed the parameter variable?


#4

Yes. When we preface a variable assignment with var we wipe any previous one from existence. The var keyword is not a part of the assignment. It is the declaration operator. What's more, when we write a variable as a parameter to a function, it is implicitly declared, just as though var were present.

In this example, name is declared in the parameter. It is a local variable that takes its definition from the value or object in the argument. We would not normally overwrite that parameter, but employ it in the function in some way..