Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions 06week/functional-javascript/hello-world.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const input = "hello world";
const upperCaser = input => {
return input.toUpperCase();
};
console.log(upperCaser(input));
90 changes: 60 additions & 30 deletions 06week/higherOrder.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,64 @@
'use strict';
"use strict";

const assert = require('assert');
const assert = require("assert");

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

function map(arr, callback) {
// Your code here
const newArr = [];
for (let item = 0; item < arr.length; item++) {
// for each item in the array, call the callback function on it
const mutatedItem = callback(arr[item]);
// push the mutated items into the new array
newArr.push(mutatedItem);
}
return newArr;
}

function filter(arr, callback) {
// Your code here
const filteredArr = [];
//for each item in the array, run the callback function and if it passes the test push original item to the filteredArr
for (let index = 0; index < arr.length; index++) {
const item = arr[index];
if (callback(item)) {
filteredArr.push(item);
}
}
return filteredArr;
}

function some(arr, callback) {
// Your code here
// Go through each item in the array until you find an item that passes the callback function
for (let i = 0; i < arr.length; i++) {
if (callback(arr[i])) {
return true;
}
}
return false;
}

function every(arr, callback) {
// Your code here
// Go through each item in the array until you find an item that does not pass the callback function
for (let i = 0; i < arr.length; i++) {
if (!callback(arr[i])) {
return false;
}
}
return true;
}

Choose a reason for hiding this comment

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

Almost all of your functions are incomplete.


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

describe('#forEach()', () => {
it('should call the callback the array.length number of times', () => {
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++;
Expand All @@ -34,70 +67,67 @@ if (typeof describe === 'function') {
});
});

describe('#map()', () => {
describe("#map()", () => {
const arr = [1, 2, 3];
const mapped = map(arr, (num) => {
const mapped = map(arr, num => {
return num * num;
});
it('should return new array with mapped items', () => {
it("should return new array with mapped items", () => {
assert.deepEqual(mapped, [1, 4, 9]);
});
it('should not affect the original array', () => {
it("should not affect the original array", () => {
assert.deepEqual(arr, [1, 2, 3]);
})
});
});

describe('#filter()', () => {
it('should return an array of items that pass the predicate test', () => {
const filtered = filter([1, 2, 3], (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.deepEqual(filtered, [2]);
});
});

describe('#some()', () => {
describe("#some()", () => {
let count = 0;
const somed = some([1, 2, 3, 4], (num) => {
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', () => {
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', () => {
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) => {
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("#every()", () => {
it("should return true if at all passes the predicate test", () => {
const everied = every([2, 4, 6], num => {
return num % 2 === 0;
});
assert.equal(everied, true);
});
let count = 0;
const everied = every([2, 3, 4, 5], (num) => {
const everied = every([2, 3, 4, 5], num => {
count++;
return num % 2 === 0;
});
it('should return false if any item fails the predicate test', () => {
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', () => {
it("should stop at the first item that fails the predicate test", () => {
assert.equal(count, 2);
});
});

} else {

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

console.log("Only run the tests on this one!");
}