1. seek array [2,6,1,7, 4] inside and the average value of all elements.
// (1) declare a variable sum sum. @ (2) traverse the array, each array element which is added to the sum inside. @ (3) with variable sum divided by the sum of the length of the array can be an array of average value. var ARR = [2,. 6,. 1,. 7,. 4 ]; var SUM = 0 ; var Average = 0 ; for ( var I = 0; I <arr.length; I ++ ) { SUM + = arr [i]; // plus the array element arr [i] is not counter I } average = sum / arr.length; the console.log (SUM, Average); // wants to output a plurality of variables, separated by commas
2, find the maximum value array [2,6,1,77,52,25,7] in
var arr = [2, 6, 1, 77, 52, 25, 7, 99]; var max = arr[0]; for (var i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } the console.log ( 'which is the maximum value of the array:' + max);
3, the array [ 'red', 'green', 'blue', 'pink'] is converted to a string, and a | or other code division
// 1. The need for a new variable for storing the conversion completes string str. // 2. traversing the original array, which are the data taken out, which was added to the string. // 3. At the same time behind one more separator var ARR = [ 'Red', 'Green', 'Blue', 'Pink' ]; var STR = '' ; var On Sep = '|' ; for ( var I = 0; I <arr.length; I ++ ) { str += arr[i] + sep; } console.log(str);
4, a new array, stored inside an integer of 10 (1-10)
// core principle: use a loop to append the array. // 1, declare an empty array arr. // 2, the loop counter i can be stored as array elements. // 3, the array index number is zero-based, and therefore more appropriate counter starting at 0, into the array element to +1. var ARR = []; for ( var I = 0; I <100; I ++ ) { // ARR = I; not directly assigned to the name of the array or previous element gone ARR [I] = I +. 1 ; } console.log(arr);
5, the array [2, 0, 6, 1, 77, 0, 52, 0, 25, 7] equal to greater than the elected element 10, into the new array.
// 1, declare a new array used to store new data newArr. // 2, traversing the original old array to find the element 10 is greater than or equal. // 3 sequentially added to the new array newArr. // Method. 1 var ARR = [2, 0,. 6,. 1, 77, 0, 52 is, 0, 25,. 7 ]; var newArr = []; var J = 0 ; for ( var I = 0; I <ARR .length; I ++ ) { IF (ARR [I]> = 10 ) { // new array index should be in ascending order starting from 0 newArr [J] = ARR [I]; j++; } } console.log(newArr); // Method 2 var ARR = [2, 0,. 6,. 1, 77, 0, 52 is, 0, 25,. 7 ]; var newArr = []; // beginning newArr.length is 0 for ( var I = 0 ; I <arr.length; I ++ ) { IF (ARR [I]> = 10 ) { // new array index should be sequentially incremented from 0 newArr [newArr.length] = ARR [I]; } } console.log(newArr);
6, the array [ 'red', 'green', 'blue', 'pink', 'purple'] Content stored in turn
// 1, declare a new array newArr // 2, the old array index No. 4 to take over (arr.length - 1), to the new index of the array element 0 (newArr.length) // 3, we taken decreasing manner i-- var ARR = [ 'Red', 'Green', 'Blue', 'Pink', 'Purple', 'HotPink' ]; var newArr = []; for ( var I = arr.length -. 1; I> = 0; i-- ) { newArr[newArr.length] = arr[i] } console.log(newArr);