Helping L2L2L with JavaScript

I do not wish to post multiple threads, for I feel as if that would be spamming. So I’ll try to contain all my question in this thread as possible. I’m usually at w3school thread. I want to expand my sources for help.

First question of many to come.(I laugh out uncontrollably with this insane nutty look on my face, as lighten and thunder flash and shout above me)

question 1:

If you know that none of the elements in your array evaluate to false in a boolean context — if your array consists only of DOM nodes, for example, you can use a more efficient idiom:


//Could someone implement this: 

colors = ['red', 'white', 'blue']; 

// into this:

var divs = document.getElementsByTagName('div');
for (var i = 0, div; div = divs[i]; i++) {
  /* Process div in some way */
}

This avoids the overhead of checking the length of the array, and ensures that the div variable is reassigned to the current item each time around the loop for added convenience.

And explain what they are conveying to me?

how is this true? Long in the console to see for yourself.

var array = ['first', 'second', , 'fourth'];

if(array[2] === undefined) { console.log('array[2] is undefined'); } // true

I can’t delete?1?!?

Any way I see how now.

Cause var array = [‘first’, ‘second’, ‘undefined’, ‘fourth’ ];

If someone could delete this post. I hate to have wasteful post that does not need reply to.

[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});

The callback would be invoked four times, with the arguments and return values in each call being as follows:

1st call: previousValue = 0 currentValue = 1 return 1

2nd call: previouValue = 1 currentValue = 2 return 3

3rd call: previousValue = 3 currentValue = 3 return 6

4th call: previousValue = 6 currentValue = 4 return 10

I think it get it… It’s just crazy.

SO if I understand, than this should be right:

var arr = [5, 6, 7, 8, 9];

arr.reduce(function(previousValue, currentValue){console.log(previousValue + currentValue)};

1st call: previousValue = 5 currentValue = 6 return 11
2nd call: previousValue = 11 currentValue = 7 return 18
3rd call: previousValue = 18 currentValue = 8 return 26
4th call: previousValue = 26 currentValue = 9 return 35

Okay, now that I work that out(In my head mind you), let’s ask the interpreter!

Yes the output return 35 in the console.

But not at first, I had to change something’s.

first I was missing syntax, and after trial and error. I correct it:

var arr = [5, 6, 7, 8, 9];
arr.reduce(function(previousValue, currentValue){console.log(previousValue + currentValue);});

or a least an output which return: 11, NaN, NaN, NaN. SO I look at the… I guest I can call it a framework… right? Or better yet just explain(someone please explain a framework in great entertaining details please!) SO I say it had return instead in console.log after inspecting it. So than I change it:

arr.reduce(function(previousValue, currentValue){return(previousValue + currentValue);});

which gave me 35 in red… But I wanted it in black! So…

var a=arr.reduce(function(previousValue, currentValue){return(previousValue + currentValue);});

console.log(a);

OUTPUT IN BLACK!

Could you please explain to me in detail why is this call reduce, and how is this reducing the array, unless they are talking about the length. I please explain!

why do the syntax look like this:

if (condition)
  statement_1
[else
  statement_2]
//and not like this:
if (condition)
  satement_1
else
  satement_2

What’s the meaning of the ?

It may not seem like a important question, but to understand the fact that one is looking for knowledge at the smallest detail, than just going off of consumption… well, if you can please answer.

In that particular example it means that part is optional.

You can have either:

if (condition)
statement_1
else
statement_2

or you can have

if (condition)
statement_1

Thank you for the clarity.

In the codes above, the name variable of the outer function is accessible to the inner functions, and there is no other way to access the inner variables except through the inner functions. The inner variables of the inner function act as safe stores for the inner functions. They hold “persistent”, yet secure, data for the inner functions to work with. The functions do not even have to be assigned to a variable, or have a name.

I see it as the inner functions act as a safe stores for the inner variables, than it being said above. This may not be a question, but it more important then that. this is speaking the concept to which one must grasp in order to completely understand JavaScript.

my second argument is that the author made an typo for this is similar to writing a tongue twister… to me that is.

Need help understanding the argument object. This is the argument object: arguments[0]? Give a short easy to read and understandable explanation on this please. It doesn’t have to be how I describe it, just please give me your take or point to a site that can give me such easy to receive information…. Or both if you can, your take and a site.


//The following code creates a two-dimensional array.
var a = new Array(4);
for (i = 0; i < 4; i++) {
  a[i] = new Array(4);
  for (j = 0; j < 4; j++) {
    a[i][j] = "[" + i + "," + j + "]";
  }
}
//This example creates an array with the following rows:
Row 0: [0,0] [0,1] [0,2] [0,3]
Row 1: [1,0] [1,1] [1,2] [1,3]
Row 2: [2,0] [2,1] [2,2] [2,3]
Row 3: [3,0] [3,1] [3,2] [3,3]
//to my understanding an nested array is:
var myarray=[[0,1][2,3]];

//Looking at the ouput on the console again, I think I understand it:
// output [object Array][Array[4], Array[4], Array[4], Array[4]]
// but would still like to have someone explain it.
// even know I can now guess that each row is referring to an array in a array of arrays...
// but still would like to have someone explain it... please.