Skip to content

Instantly share code, notes, and snippets.

@Kolenov
Created March 12, 2017 16:58
Show Gist options
  • Save Kolenov/b28f2df89a40d74f2e9007e40774d693 to your computer and use it in GitHub Desktop.
Save Kolenov/b28f2df89a40d74f2e9007e40774d693 to your computer and use it in GitHub Desktop.
Array methods
function forEach(arr, func){
"use strict";
for (var i=0, len = arr.length; i<len; i++){
arr[i] = func(arr[i]);
}
}
function every(arr, func){
"use strict";
var res = null;
for (var i=0, len = arr.length; i<len; i++){
res = res && func(arr[i]);
}
return res;
}
function some(arr, func){
"use strict";
var res = null;
for (var i=0, len = arr.length; i<len; i++){
res = res || func(arr[i]);
}
return res;
}
@Kolenov
Copy link
Author

Kolenov commented Mar 12, 2017

function forEach(arr, func){
"use strict";
for (var i=0, len = arr.length; i<len; i++){
arr[i] = func(arr[i]);
}
}

function every(arr, func){
"use strict";
var res = null;
for (var i=0, len = arr.length; i<len; i++){
res = res && func(arr[i]);
}
return res;
}

function some(arr, func){
"use strict";
var res = null;
for (var i=0, len = arr.length; i<len; i++){
res = res || func(arr[i]);
}
return res;
}

function map(arr, func){
"use strict";
var res = [];
for (var i=0, len = arr.length; i<len; i++){
res[i] = func(arr[i]);
}
return res;
}

function filter(arr, func){
"use strict";
var res = [];
for (var i=0, len = arr.length; i<len; i++){
if (func(arr[i]) === true){
res.push(arr[i]);
}
}
return res;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment