-
Notifications
You must be signed in to change notification settings - Fork 23
/
using-continue.js
41 lines (33 loc) · 1.2 KB
/
using-continue.js
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
/*
------------------------------------------------------------------------------------
Tutorial: Skipping iterations - continue instruction
------------------------------------------------------------------------------------
*/
/* Loop optimalization
Some actions in loop doesn't need to be done every iteration
You can escape loop iteration in middle of it.
Let's add all strings to make sentence.
*/
// Imagine very long array
let arr = [
'hello',
42,
108,
'number',
"world"
];
let output = ""; //Set temporary variable
for(let i = 0; i < arr.length; i++){ //Iterates trought whole array
if(typeof arr[i] != String) //If current element isn't string, we can skip processing
continue;
/*
...here may be heavy code...
*/
output += arr[i].toLowerCase() + " "; //
}
console.log(output); // Will print "hello number world", all nubers are skipped, and processing time is short
/*
------------------------------------------------------------------------------------
Challenge: create script that adds every cube of squere roots of numbers form array [2, 'car', 3, "cat", 81, true, 9]
------------------------------------------------------------------------------------
*/