Loops lesson.7 help


#1

from this how do i get it to count down and not crash my browser

for (var i = 10; i >= 1; i++) {
	console.log(i);
}

#2

This is a descending count so should decrement.

i--

#3

do you mean like this
for (var i = 10; i >= 0; i--) {
console.log(i);
}


#4

Yes. That is correct.

10
9
8
7
6
5
4
3
2
1
0

#5

good thanks so much it really helped and I'm glad it didn't crash my browser​:grinning:


#6

You're welcome. Pretty soon this will be second nature, so long as you get lots of practice and understand the components.

var myArray = [];

This is an empty array.

We can populate our array manually,

myArray[0] = 42;

The syntax above is called index subscripting. The block bracketed segment, [0] represents the index pointer, and completes the reference given by the identifier.

identifier[index] = value

We cannot attempt to access an index that does not have an associated element. The only exception to this rule is the first non-existent element, namely,

myArray[myArray.length]

and then as write-only, meaning we can assign a value. That will cement it to the array and the length will have changed. The above reference is now to the next fnee and the new element is now readable.

console.log(myArray[myArray.length - 1])

As you will learn in this unit, we rarely access an array element with literal indexes. We use loops with iterator variables that act as a dynamic index that lets us iterate the array.

The elements of an array are comma separated, and indexed sequentially from 0 to its length minus 1. If we don't subtract one we're including the fnee in the element range which will throw an error.

[ 1, 3, 5, 7 ]
  0  1  2  3

There are a few functions for mutating arrays but they don't come up till another lesson so I'll stop here.

You will learn that JavaScript array elements are like ordinary variables in that they do not have a preset data type. Each element is independent of the others and may be any data type. So we can write:

myArray = [42, 'life', [1,2,3], {planet: "Earth", galaxy: "Milky Way"}]

We can see there are four elements in the above array. A number, a string, an array of numbers, and an object literal. Ignore this for now, but it will be coming up shortly.

console.log(myArray[0]);              // 42

console.log(myArray[1]);              // life

console.log(myArray[2][0]);           // 1

console.log(myArray[2][1]);           // 2

console.log(myArray[2][2]);           // 3

console.log(myArray[3]['planet']);    // Earth

console.log(myArray[3]['galaxy']);    // Milky Way

Save this until you come back to review. Forge on...


#7

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