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
89 changes: 34 additions & 55 deletions 06week/higherOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,33 @@ function forEach(arr, callback) {
// Your code here
}

function map(arr, callback) {
function map(array, callback) {
let newArray = [];
for(let i=0; i < array.length; i++){
let elements = callback(array[i]);
newArray.push(elements);
}
return newArray;
// Your code here
}
// const checkObject = [{price: 10},{price: 20},{price: 30}];

Choose a reason for hiding this comment

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

I recommend removing unused commented-out code when you go to make your PR

// const checkArray = [10, 20, 30];
// function reduce(array, accumulator) {
// accumulator = accumulator || 0;
// for (let index = 0; index < array.length; index++) {
// if(typeof array[index] == 'number') {
// accumulator = accumulator + array[index];
// } else if(typeof array[index] == 'object') {
// for(let i in array[index]){
// accumulator = accumulator + array[index][i];
// }
// }
// }
// return accumulator;
// }
// const sum = reduce(checkArray, 10); // Set accumulator to 10
// console.log("ANSWER: ",sum);


function filter(arr, callback) {
// Your code here
Expand All @@ -23,17 +47,6 @@ function every(arr, callback) {
}

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 +60,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;
describe('#reduce()', () => {
it('should return array elements added together', () => {
const reduced = reduce([1, 2, 3], (acc, num) => {
return acc + num;
});
assert.deepEqual(filtered, [2]);
assert.deepEqual(reduced, 6);
});
});

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;
});
assert.equal(somed, false);
});
});

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!')

}