# [problem] Loop (for) trouble

• (2 Pages)
• 1
• 2

## 23 Replies - 1116 Views - Last Post: 02 November 2013 - 04:26 AM

### #16 Dormilich

• 痛覚残留

Reputation: 3794
• Posts: 11,595
• Joined: 08-June 10

## Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 10:36 AM

CasiOo, on 31 October 2013 - 06:25 PM, said:

Though I do not believe the length is being calculated each time you are doing items.length

try
```var p = document.getElementsByTagName("P");
for (var i=0; i<p.length; i++) {
document.body.appendChild(document.createElement("P"));
}

```

if that enters an infinite loop, you have the answer.

### #17 brunobbn

Reputation: 0
• Posts: 7
• Joined: 30-October 13

## Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 10:37 AM

Oh yeah in some cases it is very useful, thanks again.

### #18 brunobbn

Reputation: 0
• Posts: 7
• Joined: 30-October 13

## Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 10:46 AM

Hey guys, I have it
```else if (a == 3) {
var b = [2,3,4,5,6,7,8,9,10];
itens[3].style.backgroundColor = "#0075d3";
for (var i = 2;i<b.length;i++){
var p = b.indexOf(a);
b.splice(p,1);
itens[i].style.backgroundColor = "";

}
```

I would like to remove the element "3" from the array "b". but it removes 1 element 3 times. I wanna that the "i" sequence of values were [2,4,5,6,7,8,9,10] but it gives me [10 -1 "a" times, in this case a = 3]. I'm going crazy.

### #19 Dormilich

• 痛覚残留

Reputation: 3794
• Posts: 11,595
• Joined: 08-June 10

## Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 10:52 AM

the problem is that an unfound element in indexOf() returns –1, and –1 is a valid parameter for splice() (i.e. it counts n elements from the end of the array, instead from the beginning)

### #20 CasiOo

• D.I.C Lover

Reputation: 1510
• Posts: 3,357
• Joined: 05-April 11

## Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 10:54 AM

Dormilich, on 31 October 2013 - 05:36 PM, said:

CasiOo, on 31 October 2013 - 06:25 PM, said:

Though I do not believe the length is being calculated each time you are doing items.length

try
```var p = document.getElementsByTagName("P");
for (var i=0; i<p.length; i++) {
document.body.appendChild(document.createElement("P"));
}

```

if that enters an infinite loop, you have the answer.

That does not mean p.length is being calculated every time you need p.length
p.length is not a method, it will be changed when you are adding or removing to the collection

### #21 Dormilich

• 痛覚残留

Reputation: 3794
• Posts: 11,595
• Joined: 08-June 10

## Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 11:01 AM

Quote

That does not mean p.length is being calculated every time you need p.length

ever heard of getters*?

but how would you otherwise explain the observed behaviour of that infinite loop?

*
```var obj = {};
Object.defineProperty(obj, 'length', {
get: function() {
return (new Date).getTime();
}
});
console.log(obj.length);

```

### #22 CasiOo

• D.I.C Lover

Reputation: 1510
• Posts: 3,357
• Joined: 05-April 11

## Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 11:08 AM

No actually I did not know you could do that in Javascript

I thought the collection would observe on the DOM, listening to changes, and modify the length property accordingly

### #23 Dormilich

• 痛覚残留

Reputation: 3794
• Posts: 11,595
• Joined: 08-June 10

## Re: [problem] Loop (for) trouble

Posted 01 November 2013 - 07:50 AM

CasiOo, on 31 October 2013 - 07:08 PM, said:

I thought the collection would observe on the DOM, listening to changes, and modify the length property accordingly

but what about collections that are not attached to the DOM (like collections in a DocumentFragment) ?

### #24 CasiOo

• D.I.C Lover

Reputation: 1510
• Posts: 3,357
• Joined: 05-April 11

## Re: [problem] Loop (for) trouble

Posted 02 November 2013 - 04:26 AM

Dormilich, on 01 November 2013 - 02:50 PM, said:

CasiOo, on 31 October 2013 - 07:08 PM, said:

I thought the collection would observe on the DOM, listening to changes, and modify the length property accordingly

but what about collections that are not attached to the DOM (like collections in a DocumentFragment) ?

DocumentFragment is inheriting from HTMLNode, I see no reason why they shouldn't know when a child is being added to them
Looking at the actual source code would be the best way to find out how they actually behave