-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrainingJS10LoopStatementFor.js
More file actions
65 lines (50 loc) · 2.21 KB
/
TrainingJS10LoopStatementFor.js
File metadata and controls
65 lines (50 loc) · 2.21 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
/*
for loop is one of the most frequently used loop statements in JS. It works like while loop. In lesson 9, we wrote a while loop:
function sum1_100(){
var sum=0,num=1
while (num<=100){
sum+=num;
num++;
}
return sum;
}
We can rewrite it by using for loop, like this:
function sum1_100(){
for (var sum=0,num=1;num<=100;num++){
sum+=num;
}
return sum;
}
As you see, there are three parts in the brackets, separated by ";". The first part is the initialization variable, this part will run before the loop starts. The second part is the conditional expressions, check the expression before the start of each time loops, when the value of the expression is false, loop will be terminated. The third part is the statement about increasing and decreasing variables, they will run after the code block.
For the example above, the running order of the code is:
<--- back to part 2
-------------------------------------------
| true |
var sum=0,num=1 ---> num<=100 ? ------> code block---> num++
|
false|
loop terminated
When you need to traverse an array, you can use the for loop. look this example:
function displayElements(array){
for (var i=0;i<array.length;i++){
console.log(array[i]);
}
}
This function will display every element of array on your screen.
Ok, lesson is over, let's us do some task with for.
Task
Coding in function pickIt, function accept 1 parameter:arr, it's a number array, we need traverse arr by using for loop, if element is odd number, push it to array odd, if it's a even number, push it to array even.
I've defined two array odd and even in the function, and also wrote the return statement. your work is write a for loop.
If you forgot how to push an element to array, please refer to lesson 4.
*/
function pickIt(arr) {
let odd = [], even = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
even.push(arr[i]);
} else {
odd.push(arr[i]);
}
}
return [odd, even];
}