Why a contactsLength variable?


#1


https://www.codecademy.com/courses/building-an-address-book/0/5?curriculum_id=506324b3a7dffd00020bf661


Why is it that for this exercise we have to define a contactsLength variable?:


function list() {
	var contactsLength = contacts.length;
	for (var i = 0; i < contactsLength; i++) {
		printPerson(contacts[i]);
	}
}

Wouldn't it be simpler to just use contacts.length in the for loop?:

function list() {
	for (var i = 0; i < contacts.length; i++) {
		printPerson(contacts[i]);
	}
}

#2

@methodrockstar60195,
The course creator only wanted to point at another possibility.....

You could also have done

function list() {
 for (var i = 0,contactsLength = contacts.length; i < contactsLength; i++) {
	printPerson(contacts[i]);
 }
}

Also have a closer look at the left-hand column
properties
and
methods
in
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.