-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforInLoop.html
More file actions
123 lines (100 loc) · 3.08 KB
/
forInLoop.html
File metadata and controls
123 lines (100 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/forInLoop.css" />
<script src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="js/forInLoop.js"></script>
</head>
<body>
<h1>
Understanding literal objects and arrays in "for in" loops.
</h1>
<p>
Given...
</p>
<div class="visualize">
<div class="code">
<pre>
function printValues(thing) {
console.clear();
for (var value in thing)
{
console.log(value);
console.log(thing[value]);
}
}
</pre>
</div>
</div>
<p style="text-decoration: underline; color: blue;" onclick="jovton.printValues(person, this);">
What does a person object with simple properties contain?
</p>
<div class="visualize">
<div class="code">
<pre>
var person = {
name: "Veronica",
age: 35,
height:"1.6 meters",
sex: "female"
}
printValues(person);
</pre>
</div>
<div class="console">
<textarea>
</textarea>
</div>
</div>
<p style="text-decoration: underline; color: blue;" onclick="jovton.printValues(arrayWithObjects, this);">
What does an array of objects with just one property contain?
</p>
<div class="visualize">
<div class="code">
<pre>
var arrayWithObjects = [];
arrayWithObjects.push({name: "Veronica"});
arrayWithObjects.push({age: 36});
arrayWithObjects.push({height: "1.4 meters"});
arrayWithObjects.push({sex: "yes please!"});
printValues(arrayWithObjects);
</pre>
</div>
<div class="console">
<textarea>
</textarea>
</div>
</div>
<p style="text-decoration: underline; color: blue;" onclick="jovton.printValues(arrayOfSimpleValues, this);">
What does an array of simple values contain?
</p>
<div class="visualize">
<div class="code">
<pre>
var arrayOfSimpleValues = [];
arrayOfSimpleValues.push("name");
arrayOfSimpleValues.push("Veronica");
arrayOfSimpleValues.push("age");
arrayOfSimpleValues.push(34);
arrayOfSimpleValues.push("height");
arrayOfSimpleValues.push("1.4 meters");
arrayOfSimpleValues.push("sex")
arrayOfSimpleValues.push("female");
printValues(arrayOfSimpleValues);
</pre>
</div>
<div class="console">
<textarea>
</textarea>
</div>
</div>
<p>
<strong>
Moral of the story?
</strong>
</p>
<p style="margin-bottom: 100px;">
Use <code style="font-style:italic;"> for (int i=0; i < theArray.length; i++) </code> to iterate over an array, but <code style="font-style:italic;"> for (var property in object) </code> to iterate over an object's properties.
</p>
</body>
</html>