-
Notifications
You must be signed in to change notification settings - Fork 82
adding testing exercises #41
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 4 commits
de76c9c
5689647
ff1b382
a686bba
93c4e0b
f0fe64e
14ffca6
0d841c9
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,37 +1,93 @@ | ||
| function drop(){ | ||
|
|
||
| } | ||
|
|
||
| function fromPairs(){ | ||
|
|
||
| } | ||
|
|
||
| function head(){ | ||
|
|
||
| } | ||
|
|
||
| function take(){ | ||
|
|
||
| } | ||
|
|
||
| function takeRight(){ | ||
|
|
||
| } | ||
|
|
||
| function union(){ | ||
|
|
||
| } | ||
|
|
||
| function zipObject(){ | ||
|
|
||
| } | ||
|
|
||
| function includes(){ | ||
|
|
||
| } | ||
|
|
||
| function sample(){ | ||
|
|
||
| //Creates a slice of array with n elements dropped from the beginning. | ||
| //_.drop([1, 2, 3]); | ||
| // => [2, 3] | ||
| function drop(array, n){ | ||
| if (n === undefined){ | ||
| return array.slice(1); | ||
| } | ||
| return array.slice(n); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| function fromPairs(array) { | ||
| if(array.length === 0) { | ||
|
||
| return undefined; | ||
| } else { | ||
| var obj = {}; | ||
| for(var i = 0; i < array.length; i++) { | ||
| obj[array[i][0]] = array[i][1]; | ||
| } | ||
| return obj; | ||
| } | ||
| } | ||
|
|
||
| function head(array){ | ||
| return array.shift(); | ||
|
||
| } | ||
|
|
||
| function take(array, num){ | ||
| var newArr = []; | ||
|
||
| if (num === undefined) { | ||
| return [array[0]]; | ||
| } else if (num >= array.length) { | ||
| newArr = array; | ||
| } else { | ||
| newArr = array.slice(0, num); | ||
| } | ||
| return newArr; | ||
| } | ||
|
|
||
|
|
||
| function takeRight(array, num){ | ||
| if (num === 0) { | ||
| return []; | ||
| } else if (num >= array.length) { | ||
|
||
| return array; | ||
| } else if (num === undefined) { | ||
| return [array[(array.length - 1)]]; | ||
| } else | ||
| return array.slice(-num); | ||
| } | ||
|
|
||
| function union(arr1, arr2) { | ||
| var newArr = arr1.slice(); | ||
| for (var i = 0; i < arr2.length; i++) { | ||
| if (newArr.indexOf(arr2[i]) === -1){ | ||
| newArr.push(arr2[i]); | ||
| } | ||
| } | ||
| return newArr; | ||
| } | ||
|
|
||
| function zipObject(arr1, arr2) { | ||
| var obj = {}; | ||
| for(var i = 0; i < arr1.length; i++) { | ||
| obj[arr1[i]] = arr2[i]; | ||
| } | ||
| return obj; | ||
| } | ||
|
|
||
|
|
||
| function includes(input, value, index) { | ||
| //check type of input | ||
| if (Array.isArray(input)) { | ||
| if(index !== undefined){ | ||
| var slicedArr= input.slice(index); | ||
| return (slicedArr[0] === value); | ||
| } else { | ||
| return (input.indexOf(value) != -1); | ||
|
||
| } | ||
| } else if (typeof input === "string") { | ||
| return (input.indexOf(value) != -1); | ||
|
||
| } else { | ||
| return (Object.values(input).indexOf(value) > -1); | ||
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| function sample(arr){ | ||
| return arr[Math.floor(Math.random() * (arr.length))]; | ||
|
||
| } | ||
|
|
||
| function cloneDeep(){ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| //Write a function called productOfArray which takes in an array of numbers and returns the product of them all | ||
| //productOfArray([1,2,3]) // 6 | ||
| //productOfArray([1,2,3,10]) // 60 | ||
| function productOfArray(arr) { | ||
| //returns all elements multiplied | ||
| if (arr.length === 0) { | ||
| return 1; | ||
| } | ||
| return arr[0] * productOfArray(arr.slice(1)); | ||
| //destructive | ||
| //return arr.shift() * productOfArray(arr[]) | ||
| } | ||
| //Write a function called collectStrings which accepts an object and returns an array of all the values in the object that have a typeof string | ||
|
|
||
|
|
||
|
|
||
| function collectStrings(obj) { | ||
| var stringKeys = []; | ||
| for (var key in obj) { | ||
| if(typeof obj[key] === 'string'){ | ||
| stringKeys.push(obj[key]); | ||
| } | ||
| if (typeof obj[key] === "object") { | ||
| stringKeys = stringKeys.concat(collectStrings(obj[key])); | ||
| } | ||
| } | ||
| return stringKeys; | ||
| } | ||
|
|
||
| //Write a function called contains that searches for a value in a nested object. It returns true if the object contains that value. | ||
| var nestedObject = { | ||
| data: { | ||
| info: { | ||
| stuff: { | ||
| thing: { | ||
| moreStuff: { | ||
| magicNumber: 44 | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| //contains(nestedObject, 44) // true | ||
| //contains(nestedObject, "foo") // false | ||
| function contains(obj, value) { | ||
| return collectStrings(obj).indexOf(value) !== -1; | ||
|
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. Nice! Try to do this without using your |
||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| function replaceWith(str, l, lreplace) { | ||
| return str.split(l).join(lreplace); | ||
| } | ||
|
|
||
| //Write a function called expand which takes an array and a number and returns a copy of the array with as many numbers as specified | ||
| //expand([1,2,3],3) //[1,2,3,1,2,3,1,2,3] | ||
| function expand(arr, num) { | ||
| var newArr = []; | ||
| for(var i = 1; i <= num; i++) { | ||
| newArr = newArr.concat(arr); | ||
| } | ||
| return newArr; | ||
| } | ||
| //Write a function called mergeArrays which takes in two arrays and returns one array with the values sorted | ||
| function mergeArrays(arr1, arr2){ | ||
| var newArr = arr1.concat(arr2).sort(); | ||
| return newArr; | ||
| } | ||
|
|
||
| //Write a function called mergeObjects which takes in two objects and return an object with the keys and values combined. If the second parameter has the same key - it should override first one. There is a built in function called Object.assign - research it, but do not use it, try to do this on your own! | ||
|
|
||
| function mergeObjects(obj1, obj2) { | ||
| for (var key in obj2){ | ||
| obj1[key] = obj2[key]; | ||
| } | ||
| return obj1; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,51 @@ | ||
| var expect = chai.expect; | ||
|
|
||
| // WRITE YOUR TESTS HERE! | ||
| describe("replaceWith", function () { | ||
| it("replaces a capital with a capital", function(){ | ||
|
||
| expect(replaceWith("Foobar", "F", "B")).to.equal("Boobar"); | ||
| }); | ||
| it("does not capitalize when it isn't passed a capital", function(){ | ||
| expect(replaceWith("Hello", "e", "i")).to.equal("Hillo"); | ||
| }); | ||
| it("does not get rid of capitalization", function(){ | ||
| expect(replaceWith("TeST", "S", "M")).to.equal("TeMT"); | ||
| }); | ||
| }); | ||
|
|
||
| //Write a function called expand which takes an array and a number and returns a copy of the array with as many numbers as specified | ||
| describe("expand", function () { | ||
| it("it triples the array when given three", function(){ | ||
| expect(expand([1,2,3],3)).to.deep.equal([1,2,3,1,2,3,1,2,3]); | ||
| }); | ||
| it("does not change the array when passed one", function(){ | ||
| expect(expand(["foo", "test"],1)).to.deep.equal(["foo","test"]); | ||
| }); | ||
| }); | ||
|
|
||
| //Write a function called mergeArrays which takes in two arrays and returns one array with the values sorted | ||
| describe("mergeArrays", function () { | ||
| it("returns an array and sorts alphabetically", function(){ | ||
| expect(mergeArrays(["foo", "test"],["merge", "me"])).to.deep.equal(["foo", "me", "merge", "test"]); | ||
| }); | ||
| it("returns merged array in numerical order", function(){ | ||
| expect(mergeArrays([2, 1], [3, 4])).to.deep.equal([1, 2, 3, 4]); | ||
| }); | ||
| }); | ||
| //Write a function called mergeObjects which takes in two objects and return an object with the keys and values combined. If the second parameter has the same key - it should override first one. There is a built in function called Object.assign - research it, but do not use it, try to do this on your own! | ||
| describe("mergeObjects", function () { | ||
| it("returns an object with combined keys and values", function(){ | ||
|
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. Indentation - 2 spaces please! |
||
| var obj1 = { | ||
| name: "Foo", | ||
| num: 33 | ||
| }; | ||
| var obj2 = { | ||
| test: "thing", | ||
| num: 55 | ||
| }; | ||
| expect(mergeObjects(obj1, obj2)).to.deep.equal({ | ||
| name: "Foo", | ||
| test: "thing", | ||
| num: 55 | ||
| }); | ||
| }); | ||
| }); | ||
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.
Watch your indentation - 2 spaces please!