Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions 06week/higherOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,47 @@

const assert = require('assert');

function forEach(arr, callback) {
// Your code here
const forEach = (arr, callback) => {
for (let index = 0; index < arr.length; index++) {
callback(arr[index]);
}
}

function map(arr, callback) {
// Your code here

const map = (arr, callback) => {
const newArr = [];
for (let index = 0; index < arr.length; index++) {
newArr.push(callback(arr[index]));
}
return newArr;
}

function filter(arr, callback) {
// Your code here
const filter = (arr, callback) => {
const newArr = [];
for (let index = 0; index < arr.length; index++) {
if (callback(arr[index])) {
newArr.push(arr[index]);
}
}
return newArr;
}

function some(arr, callback) {
// Your code here
const some = (arr, callback) => {
for (let index = 0; index < arr.length; index++) {
if (callback(arr[index])) {
return true;
}
}
return false;
}

function every(arr, callback) {
// Your code here
const every = (arr, callback) => {
for (let index = 0; index < arr.length; index++) {
if (!callback(arr[index])) {
return false;
}
}
return true;
}

if (typeof describe === 'function') {
Expand Down