[problem] Loop (for) trouble

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3521
  • View blog
  • Posts: 10,163
  • Joined: 08-June 10

Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 10:36 AM

View PostCasiOo, 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.
Was This Post Helpful? 0
  • +
  • -

#17 brunobbn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#18 brunobbn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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";
		itens[3].style.borderRadius = "5px";
			for (var i = 2;i<b.length;i++){
				var p = b.indexOf(a);
				b.splice(p,1);
				alert(B)/>/>;
				itens[i].style.backgroundColor = "";
				itens[i].style.borderRadius = "";
				
		}

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.
Was This Post Helpful? 0
  • +
  • -

#19 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3521
  • View blog
  • Posts: 10,163
  • 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)
Was This Post Helpful? 1
  • +
  • -

#20 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: [problem] Loop (for) trouble

Posted 31 October 2013 - 10:54 AM

View PostDormilich, on 31 October 2013 - 05:36 PM, said:

View PostCasiOo, 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
Was This Post Helpful? 0
  • +
  • -

#21 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3521
  • View blog
  • Posts: 10,163
  • 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() {
    alert('length accessed');
    return (new Date).getTime();
  }
});
console.log(obj.length);


Was This Post Helpful? 0
  • +
  • -

#22 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • 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
Was This Post Helpful? 0
  • +
  • -

#23 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3521
  • View blog
  • Posts: 10,163
  • Joined: 08-June 10

Re: [problem] Loop (for) trouble

Posted 01 November 2013 - 07:50 AM

View PostCasiOo, 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) ?
Was This Post Helpful? 0
  • +
  • -

#24 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: [problem] Loop (for) trouble

Posted 02 November 2013 - 04:26 AM

View PostDormilich, on 01 November 2013 - 02:50 PM, said:

View PostCasiOo, 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 :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2