전체 페이지뷰

2015년 4월 14일 화요일

javascript Array function

Array function

The sort() function uses the compare() function to sort the array elements
numerically rather than lexicographically

function compare(num1, num2) {
   return num1 - num2;
}

var nums = [3,1,2,100,4,200];
nums.sort(compare);
print(nums); // 1,2,3,4,100,200
--------------------------------------------

forEach(). This function takes a function as an
argument and applies the called function to each element of an array

Iterator Functions

function square(num) {
  print(num, num * num);
}

var nums = [1,2,3,4,5,6,7,8,9,10];
nums.forEach(square);
----------------------------------------

every(), applies a Boolean function to an array
and returns true if the function can return true for every element in
the array

function isEven(num) {
   return num % 2 == 0;
}

var nums = [2,4,6,8,10];
var even = nums.every(isEven);
if (even) {
   print("all numbers are even");
}
else {
   print("not all numbers are even");
}
---------------------------------

The some() function will take a Boolean function and return true if at least
one of the elements in the array meets the criterion of the Boolean function

function isEven(num) {
   return num % 2 == 0;
}

var nums = [1,2,3,4,5,6,7,8,9,10];
var someEven = nums.some(isEven);
if (someEven) {
   print("some numbers are even");
}
else {
   print("no numbers are even");
}
nums = [1,3,5,7,9];
someEven = nums.some(isEven);
if (someEven) {
   print("some numbers are even");
}
else {
   print("no numbers are even");
}
----------------------------------------
The reduce() function applies a function to an accumulator and the successive elements of an array until the end of the array is reached, yielding a single value.

function add(runningTotal, currentValue) {
   return runningTotal + currentValue;
}

var nums = [1,2,3,4,5,6,7,8,9,10];
var sum = nums.reduce(add);
print(sum); // displays 55

function concat(accumulatedString, item) {
   return accumulatedString + item;
}

var words = ["the ", "quick ","brown ", "fox "];
var sentence = words.reduce(concat);
print(sentence); // displays "the quick brown fox"

------------------------------------------------------
The map() function works like the forEach() function, applying a function to
each element of an array. The difference between the two functions is that map()
returns a new array with the results of the function application.

function curve(grade) {
   return grade += 5;
}

var grades = [77, 65, 81, 92, 83];
var newgrades = grades.map(curve);
print(newgrades); // 82, 70, 86, 97, 88

-------------------------------------------------------
The filter() function works similarly to every(), but instead of returning
true if all the elements of an array satisfy a Boolean function, the function
returns a new array consisting of those elements that satisfy the Boolean function

function passing(num) {
   return num >= 60;
}

var grades = [];
for (var i = 0; i < 20; ++i) {
    grades[i] = Math.floor(Math.random() * 101);
}
var passGrades = grades.filter(passing);
print("All grades: );
print(grades);
print("Passing grades: ");
print(passGrades);

Creating Two-Dimensional Arrays

Array.matrix = function(numrows, numcols, initial) {
   var arr = [];
   for (var i = 0; i < numrows; ++i) {
      var columns = [];
      for (var j = 0; j < numcols; ++j) {
         columns[j] = initial;
      }
      arr[i] = columns;
   }
   return arr;
}

var nums = Array.matrix(5,5,0);
print(nums[1][1]); // displays 0
var names = Array.matrix(3,3,"");
names[1][2] = "Joe";
print(names[1][2]); // display "Joe"


this contents is copied by data structures and algorithms with javascript

댓글 없음:

댓글 쓰기