1var data = [1, 2, 3, 4, 5, 6];
2
3// traditional for loop
4for(let i=0; i<=data.length; i++) {
5 console.log(data[i]) // 1 2 3 4 5 6
6}
7
8// using for...of
9for(let i of data) {
10 console.log(i) // 1 2 3 4 5 6
11}
12
13// using for...in
14for(let i in data) {
15 console.log(i) // Prints indices for array elements
16 console.log(data[i]) // 1 2 3 4 5 6
17}
18
19// using forEach
20data.forEach((i) => {
21 console.log(i) // 1 2 3 4 5 6
22})
23// NOTE -> forEach method is about 95% slower than the traditional for loop
24
25// using map
26data.map((i) => {
27 console.log(i) // 1 2 3 4 5 6
28})
1const array = ["one", "two", "three"]
2array.forEach(function (item, index) {
3 console.log(item, index);
4});
1function filteredArray(arr, elem) {
2 let newArr = [];
3 // change code below this line
4
5 for (let i = 0; i < arr.length; i++) {
6 if (arr[i].indexOf(elem) == -1) {
7 //Checks every parameter for the element and if is NOT there continues the code
8 newArr.push(arr[i]); //Inserts the element of the array in the new filtered array
9 }
10 }
11
12 // change code above this line
13 return newArr;
14}
15// change code here to test different cases:
16console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
17
1let numbers = [1,2,3,4,5];
2let numbersLength = numbers.length;
3for ( let i = 0; i < numbersLength; i++) {
4 console.log (numbers[i]);
5}
1var arr = [10, 9, 8, 7, 6];
2for (var i = 0; i < arr.length; i++) {
3 console.log(arr[i]);
4}
5
1var arr = [10, 9, 8, 7, 6];
2for (var i = 0; i < arr.length; i++) {
3 console.log(arr[i]);
4}