<script src= "Prototype.js" ></script>
<script>
/**//*var arr = [1,2,3,4,8,5,4,3];
Output 1,2,3,4,5,4,3 sequentially
Arr.each (
function (item, index) {
if (item < 6) {
alert (item);
} else {
Throw $continue;
}
}
);
Output 1,2,3,4 sequentially
Arr.each (
function (item, index) {
if (item < 6) {
alert (item);
} else {
Throw $break;
}
}
);*/
/**//*
var arr = [1,2,3,4,5];
arr = [1,2,3,4,5]
Arr.reverse (FALSE);
Alert (Arr.inspect ());
arr = [5,4,3,2,1]
Arr.reverse ();
Alert (Arr.inspect ());
var arr = [1,2,3,4,5];
Arr.reverse ();
Returns the number of 2 in ARR: 1
var index = Arr.indexof (2);
Alert (Index)
var arr = [1,2,3,4,5];
does not contain 2 and 4, return [1,3,5]
var newArr = arr.without (2,4);
Alert (Newarr.inspect ());
var arr = [1,2,3,4,5];
Arr.clear ();
Alert (arr[2]);
var arr = [1,2,3,4,5];
var arr2 = [2,4,6,8,10];
[[1,2],[2,4],[3,6],[4,8],[5,10]]
var newArr = Arr.zip (ARR2);
[[1,4],[4,16],[9,36],[16,64],[25,100]]
var newArr2 = Arr.zip (
ARR2,
function (item) {
var newitem = Item.collect (
function (item, index) {
return item * Item;
}
);
return newitem;
}
);
Alert (Newarr.inspect ());
Alert (Newarr2.inspect ());
var arr = [5,2,3,1,4];
Sort the arr
var arr = Arr.sortby (
function (item, index) {
return item;
}
);
Arr.each (
function (item, index) {
alert (item);
}
);
var arr = [
{root:1, square:1},
{root:2, square:4},
{root:3, square:9},
{root:4, square:16},
{root:5, square:25}
];
var newArr = Arr.pluck ("square");
Alert (newarr[4]);
var arr = [1,2,3,4,5];
var Ptarr = arr.partition (
function (item, index) {
return item%2 = = 1;
}
);
var oddarr = ptarr[0];
var evenarr = ptarr[1];
Oddarr.each (
function (item) {
alert (item);
}
);
Evenarr.each (
function (item) {
alert (item);
}
);
var arr = [1,2,3,4,5];
Maximum Value
var max =-arr.min (
function (item, index) {
Return-item;
}
);
Minimum value
var min = arr.min ();
Alert (max);
Alert (min);
var arr = [1,2,3,4,5];
Maximum Value
var max = Arr.max ();
Minimum value
var min =-arr.max (
function (item, index) {
Return-item;
}
);
Alert (max);
Alert (min);
To find the square of each element in a set
Number.prototype.square = function () {
return this *;
}
var arr = [1,2,3,4,5];
var newArr = Arr.invoke ("square");
Alert (newarr[4]);
Calculate the product in the ARR
var factorial = Arr.inject (
1,
function (accumulator, item, index) {
return accumulator * Item;
}
);
Alert (factorial)
To determine if the collection contains "2"
var inc = Arr.include (2);
Alert (INC);
var arr = ["12345", "ABC2", "CDE", "FGH", "132BA"];
var NewArray = Arr.grep (
/2/,
function (item, index) {
Return item + "contains 2";
}
)
Newarray.each (
function (item) {
alert (item);
}
);
var arr = [1,2,3,4,5];
Returns all the odd numbers in the collection
var Oddarr = Arr.findall (
function (item, index) {
return item%2 = = 1;
}
);
Alert (oddarr[2]);
Returns the first element greater than 3
var ele = Arr.find (
function (item, index) {
Return (item > 3);
}
);
alert (ele);
var newArr = Arr.collect (
function (item, index) {
Return item * (index + 1);
}
);
var hasnegative = Arr.any (
function (item, index) {
Return (item < 0);
}
);
Tests whether all elements in the collection are greater than 0
var allpositive = Arr.all (
function (item, index) {
Return (item > 0);
}
)
alert (allpositive);
Arr.each (
function (item, index) {
Alert ("arr[" + index + "] =" + Item);
}
);*/
</script>