Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
First Question:
every:
JavaScript
function (list, iterator) {
    if (list == null) return true;
    	iterator = iterator || this.identity;
    	var keys = list.length !== +list.length && this.keys(list),
    		length = (keys || list).length,
    		index, currentKey;
    	for (index = 0; index < length; index++) {
    		currentKey = keys ? keys[index] : index;
    		if (!iterator(list[currentKey], currentKey, list)) return false;
    	}
    	return true;
    
    }



Second Question (no need for line by line instruction):

each:
JavaScript
function (list, iterator) {
    var i, length;
    if (isArrayLike(list)) {
    	for (i = 0, length = list.length; i < length; i++) {
    		iterator(list[i], i, list);
    	}
    }
    else {
    	var keys = Object.keys(list);
    	for (i = 0, length = keys.length; i < length; i++) {
    		iterator(list[keys[i]], keys[i], list);
    	}
    }
}


How come in this function you have to do the iterator over all three of the (list[i], i, and list)?

Thanks!
Posted
Updated 17-Sep-15 5:07am
v2

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
 
Share this answer
 
Comments
[no name] 17-Sep-15 10:50am    
Ok, I hear you. I'm new at this and really just looking for a little explanation. But I understand it would take a while. What I really don't get are these lines:

First Question:
every: function (list, iterator) {
if (list == null) return true;
iterator = iterator || this.identity;
var keys = list.length !== +list.length && this.keys(list) <---
length = (keys || list).length,
index, currentKey;
for (index = 0; index < length; index++) {
currentKey = keys ? keys[index] : index;
if (!iterator(list[currentKey], currentKey, list)) return false; <---
}
return true;

},
Patrice T 17-Sep-15 11:04am    
+5
Go to http://www.w3schools.com/js/default.asp[^] and you will be able to learn all about it.
 
Share this answer
 
What you request is a huge work because of your little understanding of this code. The fact that you ask this makes that you will need a lot of explanations.

First solution:
- Hire a consultant or ask a friend/teacher more skilled than you.

Second Solution:
- Use the debugger to see the code execute, you will see what it does. It may be enough for you to understand it. By seeing what it does, you may understand why.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900