-
Notifications
You must be signed in to change notification settings - Fork 83
finished tests; working on implementing functions #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 12 commits
a7a05c8
f461d1a
2068677
1828583
a458e2d
39ef109
24b3ca6
f621f0a
f42f98e
a772b65
ca2195c
0d0e826
ef62251
2beb2c8
fc8cca5
e8ea7cd
f986863
608e512
1c272a4
8198184
d0f6d3e
203b72d
7dc77a3
4cdb916
069f8f6
c1c102d
97f33d1
d3084ba
19b05ab
c39642a
3284548
a4e9826
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,98 @@ | ||
| function drop(){ | ||
|
|
||
| function drop(arr, num=1){ | ||
| if (num === 1) { | ||
| return arr.slice(1); | ||
| } else { | ||
| return arr.slice(num); | ||
| } | ||
| } | ||
|
|
||
| function fromPairs(){ | ||
|
|
||
| function fromPairs(arr){ | ||
| // assuming arr is an array of arrays containing key value pairs | ||
| // e.g. [[key, value],[key, value],[key, value]] | ||
| if (arr.length === 0) { | ||
| return {}; | ||
| } else { | ||
| var result = {}; | ||
| for (var i = 0; i < arr.length; i++) { | ||
| result[arr[i][0]] = arr[i][1]; | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| function head(){ | ||
|
|
||
| function head(arr){ | ||
| if (arr.length === 0) { | ||
| return undefined; | ||
| } else { | ||
|
||
| return arr[0]; | ||
| } | ||
| } | ||
|
|
||
| function take(){ | ||
|
|
||
| function take(arr, num=1){ | ||
| return arr.slice(0, num); | ||
| } | ||
|
|
||
| function takeRight(){ | ||
|
|
||
| function takeRight(arr, num=1){ | ||
| if (num === 0) { | ||
| return [] | ||
| } else { | ||
|
||
| return arr.slice(num*-1); | ||
| } | ||
| } | ||
|
|
||
| function union(){ | ||
|
|
||
| } | ||
|
|
||
| function zipObject(){ | ||
|
|
||
| } | ||
|
|
||
| function includes(){ | ||
|
|
||
| } | ||
|
|
||
| function sample(){ | ||
|
|
||
| } | ||
|
|
||
| function cloneDeep(){ | ||
|
|
||
| // assuming arguments will be an "array" of arrays | ||
| var result = []; | ||
| for (var i = 0; i < arguments.length; i++) { | ||
| for (var j = 0; j < arguments[i].length; j++) { | ||
| if (result.indexOf(arguments[i][j]) === -1) { | ||
| result.push(arguments[i][j]); | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function zipObject(keysArr, valuesArr){ | ||
| var result = {}; | ||
| for (var i = 0; i < keysArr.length; i++) { | ||
| result[keysArr[i]] = valuesArr[i]; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function includes(collection, val, fromIndex=0){ | ||
| // collection can be an array, string, or object | ||
| if (typeof collection === "object" && Array.isArray(collection) !== true) { | ||
| for (var key in collection) { | ||
| if (collection[key] === val) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| return (collection.slice(fromIndex).indexOf(val) !== -1) ? true : false; | ||
|
||
| } | ||
|
|
||
| function sample(arr){ | ||
| return arr[Math.floor(Math.random()*arr.length-1)]; | ||
| } | ||
|
|
||
| function cloneDeep(collection){ | ||
| var result; | ||
| if (Array.isArray(collection)) { | ||
| result = []; | ||
| collection.forEach(function(val){ | ||
| // we want to make all the references different | ||
| result.concat(cloneDeep(val)); | ||
| }); | ||
| } else { | ||
| result = {}; | ||
| for (var key in collection) { | ||
| result[key] = collection[key]; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function sumBy(){ | ||
|
|
@@ -82,6 +139,8 @@ function flatten(){ | |
|
|
||
| } | ||
|
|
||
| // BONUS | ||
|
|
||
| function zip(){ | ||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| function productOfArray(arr) { | ||
| if (arr.length === 1) { | ||
| return arr[0]; | ||
| } | ||
| return arr[0] * productOfArray(arr.slice(1)); | ||
| } | ||
|
|
||
| function collectStrings(obj) { | ||
| var strings = []; | ||
| function collectStringsHelper(obj) { | ||
| for (var key in obj) { | ||
| if (typeof(obj[key]) === "string") { | ||
| strings.push(obj[key]); | ||
| } else { | ||
| collectStringsHelper(obj[key]); | ||
| } | ||
| } | ||
| } | ||
| collectStringsHelper(obj); | ||
| return strings; | ||
| } | ||
|
|
||
| function contains(nestedObj, searchTerm) { | ||
| for (var key in nestedObj) { | ||
| if (typeof(nestedObj[key]) === "object") { | ||
| if (contains(nestedObj[key], searchTerm)) { | ||
| return true; | ||
| }; | ||
| } else if (nestedObj[key] === searchTerm) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| function search(arr, val, currentIndex=0) { | ||
| if (arr[0] === val) { | ||
| return currentIndex; | ||
| } | ||
| // if an empty array is passed in, value is not found | ||
| else if (arr.length === 0) { | ||
| return -1; | ||
| } | ||
| // otherwise, increment the index to keep track of it, and then remove the | ||
| // value that was already checked, and pass in a shorter array to search() | ||
| else { | ||
| currentIndex++; | ||
| currentIndex = search(arr.slice(1), val, currentIndex); | ||
| } | ||
| return currentIndex; | ||
| } | ||
|
|
||
| function binarySearch(arr, val) { | ||
| // sort the array | ||
|
||
| // find the middle, compare | ||
| // remove the middle and everything before or after it | ||
| // depending on the comparison | ||
| // repeat | ||
| } | ||
|
|
||
| function stringifyNumbers(obj) { | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| function replaceWith(str, replaceThisChar, replacementChar) { | ||
| var arrayOfChars = str.split(""); | ||
| arrayOfChars.forEach(function(val, index){ | ||
|
||
| if (val === replaceThisChar) { | ||
| arrayOfChars[index] = replacementChar; | ||
| } | ||
| }); | ||
| return arrayOfChars.join(""); | ||
| } | ||
|
|
||
| function expand(arr, num) { | ||
| if (num === 1) { | ||
|
||
| return arr; | ||
| } | ||
| var result = arr; | ||
| for (var i = 1; i < num; i++) { | ||
| result = result.concat(arr); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function acceptNumbersOnly() { | ||
| for (var i = 0; i < arguments.length; i++) { | ||
| if (typeof(arguments[i]) !== "number" || isNaN(arguments[i])) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check out the method |
||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| function mergeArrays(arr1, arr2) { | ||
| var mergedArr = arr1.concat(arr2); | ||
| return mergedArr.sort(function(a, b){ | ||
| return a - b; | ||
| }); | ||
| } | ||
|
|
||
| function mergeObjects(obj1, obj2) { | ||
| var result = {}; | ||
| for (var key in obj1) { | ||
| result[key] = obj1[key]; | ||
| } | ||
| for (var key in obj2) { | ||
| result[key] = obj2[key]; | ||
| } | ||
| return result; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,71 @@ | ||
| var expect = chai.expect; | ||
|
|
||
| // WRITE YOUR TESTS HERE! | ||
| describe("replaceWith", function(){ | ||
| it("returns a string", function(){ | ||
| expect(replaceWith("test", "t", "x")).to.be.a("string"); | ||
| }); | ||
| it("removes the designated character", function(){ | ||
| expect(replaceWith("test", "t", "x")).to.not.have.string("t"); | ||
| }); | ||
| it("inserts the designated character", function(){ | ||
| expect(replaceWith("test", "t", "x")).to.have.string("x"); | ||
| }); | ||
| it("replaces specific char with designated char", function() { | ||
| expect(replaceWith("awesome", "e", "z")).to.deep.equal("awzsomz"); | ||
| expect(replaceWith("Foo", "F", "B")).to.deep.equal("Boo"); | ||
| }); | ||
| it("is case sensitive", function(){ | ||
| expect(replaceWith("aBcBbBbBb", "B", "c")).to.deep.equal("acccbcbcb"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("expand", function(){ | ||
| it("returns an array", function(){ | ||
| expect(expand([1,2,3],3)).to.be.an("array"); | ||
| }); | ||
| it("should return an array that contains the original array a number of times", function(){ | ||
| expect(expand([1,2,3],3)).to.include.members([1,2,3]); | ||
| expect(expand([1,2,3],3)).to.deep.equal([1,2,3,1,2,3,1,2,3]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("acceptNumbersOnly", function(){ | ||
| it("returns a boolean", function(){ | ||
| expect(acceptNumbersOnly(1,2,3,4,5,6,7)).to.equal(true); | ||
| expect(acceptNumbersOnly(1,2,3,4,5,6,"hi")).to.equal(false); | ||
| }); | ||
| it("returns true if all items in array are numbers", function(){ | ||
| expect(acceptNumbersOnly(1,2,3,4,5,6,7)).to.be.true; | ||
| expect(acceptNumbersOnly(100,200,300,400)).to.be.true; | ||
| }); | ||
| it("returns false if not all items numbers", function(){ | ||
| expect(acceptNumbersOnly(1, "foo")).to.be.false; | ||
| }); | ||
| it("returns false if NaN is present", function(){ | ||
| expect(acceptNumbersOnly(1,2,3,4,5,6,NaN)).to.be.false; | ||
| }); | ||
| }); | ||
|
|
||
| describe("mergeArrays", function(){ | ||
| it("returns an array", function(){ | ||
| expect(mergeArrays([2,1],[3,4])).to.be.an("array"); | ||
| }); | ||
| it("returns the array sorted", function(){ | ||
| expect(mergeArrays([2,1],[3,4])).to.deep.equal([1,2,3,4]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("mergeObjects", function(){ | ||
| it("should return an object", function(){ | ||
| expect(mergeObjects({hello:1, bye:2}, {hello2:3, bye2:4})).to.be.an("object"); | ||
| }); | ||
| it("should override value in first passed in obj if both objs have the same key", function(){ | ||
| expect(mergeObjects({hello:1, bye:2}, {hello:3, bye:4})).to.deep.equal({hello:3, bye:4}); | ||
| }); | ||
| it("should return an object with all the keys from both objs", function(){ | ||
| expect(mergeObjects({hello:1, bye:2}, {hello2:3, bye2:4})).to.have.all.keys("hello", "bye", "hello2", "bye2"); | ||
| }); | ||
| it("should not change the key-value pairs", function(){ | ||
| expect(mergeObjects({hello:1, bye:2}, {hello2:3, bye2:4})).to.deep.equal({hello:1, bye:2, hello2:3, bye2:4}); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need the
elsehere since you're returning in both cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.