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 i = 0 ; i < arr.length ; i++){
callback(arr[i])
}
}

function map(arr, callback) {
// Your code here
const map = (arr, callback) => {
const newArr = [];
for(let i = 0 ; i< arr.length ; i++ ){
const formattedItem=callback(arr[i],i,arr)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless your using the extra arguments . You should only pass one index of the array at a time.

newArr.push(formattedItem)
}
return newArr
}

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

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

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

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