Skip to content

Commit

Permalink
last-minute test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
peterkhayes committed Jul 15, 2014
1 parent b292b75 commit c3a84aa
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 58 deletions.
78 changes: 41 additions & 37 deletions FunctionalProgramming/spec/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,52 +50,23 @@ describe("Basic building blocks", function() {
var result = reduce([1, 2, 3, 4, 5], function(partial, elem) {return partial + elem}, 0)
expect(result).to.equal(15);
});

it("concats strings", function() {
var result = reduce(["bi", "bim", "bap"], function(partial, elem) {return partial + elem}, '');
expect(result).to.equal("bibimbap");
})
});
});

describe("More specialized functions", function() {

describe("every", function() {
expect(every([1, 2, 3, 4, 5, 6], function(x) {return x < 10})).to.be(true);
expect(every([1, 2, 3, 4, 25, 6], function(x) {return x < 10})).to.be(false);
});

describe("some", function() {
expect(every([1, 2, 3, 4, 25, 6], function(x) {return x > 10})).to.be(true);
expect(every([1, 2, 3, 4, 5, 6], function(x) {return x > 10})).to.be(false);
});

describe("unique", function() {
var result = unique(["a", "ab", "a", "b", "ab", "ba"]);
assertArrayEquals(result, ["a", "ab", "b", "ba"]);
});

describe("flatten", function() {
var result = flatten([1, 2, [3, [4]]]);
assertArrayEquals(result, [1, 2, 3, 4]);
});

describe("contains", function() {
expect(contains([1, 2, 3, 4], 1)).to.be(true);
expect(contains([1, 2, 3, 4], 0)).to.be(false);
});
});

});

checkIfFunctionalSolution = function(func) {
func = func.toString()
func = func.toString();
expect(func).to.not.contain("for(");
expect(func).to.not.contain("for (");
expect(func).to.not.contain("if(");
expect(func).to.not.contain("if (");
expect(func).to.not.contain("while(");
expect(func).to.not.contain("while (");
}
};

describe("Applied problems", function() {
it("Sum an array", function() {
Expand All @@ -109,7 +80,8 @@ describe("Applied problems", function() {
});

it("Counting the number of unique first names in this sequence", function() {
assertArrayEquals(uniqueFirstNames(["John Bonham", "Roger Waters", "John Lennon", "Nick Moon", "Roger Daltry", "Nick Cave", "Jimmy Hendrix", "Jimmy Buffet", "Jimmy Page", "Frank Zappa"]), 5);
var result = uniqueFirstNames(["John Bonham", "Roger Waters", "John Lennon", "Nick Moon", "Roger Daltry", "Nick Cave", "Jimmy Hendrix", "Jimmy Buffet", "Jimmy Page", "Frank Zappa"]);
expect(result).to.equal(5);
checkIfFunctionalSolution(uniqueFirstNames);
});

Expand All @@ -119,7 +91,9 @@ describe("Applied problems", function() {
});

it("Indexing strings by their length", function() {
assertArrayEquals(indexByLength(["hello", "satan", "this", "is", "your", "dog"]), [undefined, undefined, ["is"], ["dog"], ["this", "your"], ["hello", "satan"]]);
var result = indexByLength(["hello", "satan", "this", "is", "your", "dog"]);
var answer = [undefined, undefined, ["is"], ["dog"], ["this", "your"], ["hello", "satan"]];
expect(JSON.stringify(result)).to.equal(JSON.stringify(answer));
checkIfFunctionalSolution(indexByLength);
});

Expand All @@ -129,9 +103,39 @@ describe("Applied problems", function() {
{name: "Sal", age: 59, children: ["Sam", "Sally"]},
{name: "Rita", age: 66, children: ["Rob"]},
{name: "Linda", age: 56, children: ["Rick", "James", "Jose"]}
]
var filtered = olderOrWithChildren(people)
];
var filtered = olderOrWithChildren(people);
assertArrayEquals(filtered, ["Fred", "Sal", "Linda"]);
checkIfFunctionalSolution(olderOrWithChildren);
});
});


describe("More specialized functions", function() {

describe("every", function() {
expect(every([1, 2, 3, 4, 5, 6], function(x) {return x < 10})).to.be(true);
expect(every([1, 2, 3, 4, 25, 6], function(x) {return x < 10})).to.be(false);
});

describe("some", function() {
expect(every([1, 2, 3, 4, 25, 6], function(x) {return x > 10})).to.be(true);
expect(every([1, 2, 3, 4, 5, 6], function(x) {return x > 10})).to.be(false);
});

describe("unique", function() {
var result = unique(["a", "ab", "a", "b", "ab", "ba"]);
assertArrayEquals(result, ["a", "ab", "b", "ba"]);
});

describe("flatten", function() {
var result = flatten([1, 2, [3, [4]]]);
assertArrayEquals(result, [1, 2, 3, 4]);
});

describe("contains", function() {
expect(contains([1, 2, 3, 4], 1)).to.be(true);
expect(contains([1, 2, 3, 4], 0)).to.be(false);
});

});
54 changes: 33 additions & 21 deletions FunctionalProgramming/src/problems.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,6 @@ var reduce = function(input, iterator, initialValue) {

};


/* -------------------------- */
/* MORE SPECIALIZED FUNCTIONS */
/* -------------------------- */

var every = function(input, iterator) {

};

var some = function(input, iterator) {

};

var unique = function(input) {

};

var flatten = function(input) {

};

/* ---------------- */
/* APPLIED PROBLEMS */
/* ---------------- */
Expand Down Expand Up @@ -82,5 +61,38 @@ var olderOrWithChildren = function(input) {



/* -------------------------- */
/* MORE SPECIALIZED FUNCTIONS */
/* -------------------------- */

// Given an iterator that returns true or false, return true
// if every input element passes the test and false if at least one fails.
var every = function(input, iterator) {

};

// Given an iterator that returns true or false, return true
// if at least one input element passes the test and false if all of them fail.
var some = function(input, iterator) {

};

// Returns an array with duplicate elements in the input removed.
var unique = function(input) {

};

// un-nests an array. that is, flatten([[1, 2], [3], [[4]]])
// becomes [1, 2, 3, 4]
var flatten = function(input) {

};

// returns true if the input contains the target, and false if not.
var contains = function(input, target) {

};




Binary file removed Matching/img/Bernie.jpeg
Binary file not shown.
Binary file removed Matching/img/Breakfast.jpeg
Binary file not shown.
Binary file removed Matching/img/Colin.jpeg
Binary file not shown.
Binary file removed Matching/img/Fredrick.jpeg
Binary file not shown.
Binary file removed Matching/img/Rex.jpeg
Binary file not shown.

0 comments on commit c3a84aa

Please sign in to comment.