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
119 changes: 52 additions & 67 deletions 06week/higherOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,57 @@

const assert = require('assert');

function forEach(arr, callback) {
// Your code here
}

const arr = [10,20,30];
function map(arr, callback) {
// Your code here
}
//create an empty array into which you will push the new elements
let newArray = [];
//loop through the original array
for (let i=0;i<arr.length;i++){
//define a new variable equal to each element in the original array, each one will have a callback function applied to it
let elements = callback(arr[i]);
//push all of the new variables into the new, previously empty array
newArray.push(elements);
}
//return new array with new elements
return newArray;
};

function filter(arr, callback) {
// Your code here
function reduce(array, callback, accumulator){
//sets accumulator equal to itself if that is truthy, or equal to zero if it is not truthy
accumulator = accumulator || 0;
//looping through an array, one index position at a time
for (let index=0; index<array.length; index++){
//if the element in the array is a number type, then set the variable equal to itself plus the element's value
if (typeof array[index] == 'number') {
accumulator = accumulator + array[index];
//otherwise, if the element in the array is an object, loop through the object properties to find the key value and add that to the accumulator
} else if (typeof array[index] == 'object') {
for (let i in array[index]){
accumulator = accumulator + array[index][i]
}
}
//then you create a function called a callback function that accepts all of these parameters
callback(accumulator, array[index], index, array);
}
//return the accumulator variable, or the total value of the callback function
return accumulator;
}

function some(arr, callback) {
// Your code here
}

function every(arr, callback) {
// Your code here
function filter(arr, callback) {
//create an empty array
let filteredArr = [];
//loop through the original array
for (let i=0;i<arr.length; i++){
//checks to see if the conditions of the callback function are true, pushes values to new array if so
if (callback(arr[i],i, arr) == true){

Choose a reason for hiding this comment

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

this line could just be if (callback(arr[i],i, arr)){
because it will already equate to true or false

filteredArr.push(arr[i]);
}
}
return filteredArr;
}

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

describe('#forEach()', () => {
it('should call the callback the array.length number of times', () => {
let count = 0;
forEach([1, 2, 3], () => {
count++;
});
assert.equal(count, 3);
});
});

describe('#map()', () => {
const arr = [1, 2, 3];
const mapped = map(arr, (num) => {
Expand All @@ -47,57 +66,23 @@ if (typeof describe === 'function') {
})
});

describe('#filter()', () => {
it('should return an array of items that pass the predicate test', () => {
const filtered = filter([1, 2, 3], (num) => {
return num % 2 === 0;
});
assert.deepEqual(filtered, [2]);
});
});

describe('#some()', () => {
let count = 0;
const somed = some([1, 2, 3, 4], (num) => {
count++;
return num % 2 === 0;
});
it('should return true if at least one item passes the predicate test', () => {
assert.equal(somed, true);
});
it('should stop at the first item that passes the predicate test', () => {
assert.equal(count, 2);
});
it('should return false if no items pass the predicate test', () => {
const somed = some([1, 3, 5], (num) => {
return num % 2 === 0;
describe('#reduce()', () => {
it('should return array elements added together', () => {
const reduced = reduce([1, 2, 3], (acc, num) => {
return acc + num;
});
assert.equal(somed, false);
assert.deepEqual(reduced, 6);
});
});

describe('#every()', () => {
it('should return true if at all passes the predicate test', () => {
const everied = every([2, 4, 6], (num) => {
describe('#filter()', () => {
it('should return an array of items that pass the predicate test', () => {
const filtered = filter([1, 2, 3], (num) => {
return num % 2 === 0;
});
assert.equal(everied, true);
});
let count = 0;
const everied = every([2, 3, 4, 5], (num) => {
count++;
return num % 2 === 0;
});
it('should return false if any item fails the predicate test', () => {
assert.equal(everied, false);
});
it('should stop at the first item that fails the predicate test', () => {
assert.equal(count, 2);
assert.deepEqual(filtered, [2]);
});
});

} else {

console.log('Only run the tests on this one!')

}