Correct me if I’m wrong, but isn’t the bottom “wordBlanks” variable an object?
I’m not sure I understand what you mean. I see no objects there and didn’t use any in my answer. (Unless you want to get nitpicky and point out that in JS, functions are objects.) I think you are referring to the function call.
Why even declare result initially as =“”?
It is common to declare something as an empty string and then append to it. I do this from time to time.
And then, why is wordBlanks at the bottom. How does Javascript execute this?
A function declaration is different from it’s calling. If I declare a function:
function myFunc() {
console.log("Howdy, parder!");
}
This does nothing but allocated some memory with some instructions in it. In order to get it to do something, I need to call it:
function myFunc() {
console.log("Howdy, parder!");
}
myFunc();
or if I want to pass a parameter:
function myFunc(name) {
console.log("Howdy, " + name + "!");
}
myFunc("pardner");
This is the same schema as “Word Blanks”.
but I’m somehow supposed to know suddenly function and what result=“”
It is unfortunate that the section “Write Reusable JavaScript with Functions” comes after. I can’t remember if functions are ever encountered before that. If they weren’t then that is unfortunate and maybe it will get addressed in the beta. I’m sure you can find a youtube video by searching “javascript functions”. The basic concept is not hard, but I understand if it was a shock.
As to result=""
, that is just creating a blank string to which you can add things. For example:
var myString = ""; // declared as an empty string, nothing in it
for (var i = 10; i > 0; i--) {
myString += i + ", ";
}
myString += "BLAST OFF!!!";
console.log(myString);
// output -> 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, BLAST OFF!!!
This is a common thing to do. Why do var myString = "";
instead of just var myString;
? That way I know it is a string value and will be treated as such. Often it wouldn’t matter. But imaging if, for some strange reason, I needed to do something like this:
var myString; // declared as an empty string, nothing in it
for (var i = 10; i > 0; i--) {
myString += i
myString += ", ";
}
myString += "BLAST OFF!!!";
console.log(myString);
// output -> NaN, 9, 8, 7, 6, 5, 4, 3, 2, 1, BLAST OFF!!!
Notice that there is no “10”, it is NaN (or Not a Number), when we got there the first time, it didn’t know what myString was. Is it a number? Is it a string? JS has to know this to know how to treat it. When we get to myString += ", ";
, JS goes Ah ha! It now assumes it’s a string because you are trying to add (concatenate) a string to it. All of this confusion could have been avoided if I’d used var myString = "";
JS is a little light and loose with how it treats data types (compared to other languages where this would never be an issue) so it is always good to be extra cautious.
Yes, fCC can be a little frustrating. Yes, you will be hit with a new challenge that you feel that fCC has not prepared you for (I’m on the back end section and just got hit with a big one.) But remember that as a programmer, that is how it works. Even the best programmer in the world does not know everything. And new frameworks keep getting added at an alarming rate. There will be assignments where you will be asked to do something and you won’t even know where to start. Learn to research. Learn to ask. And remember that we are here to help you.