-
Notifications
You must be signed in to change notification settings - Fork 83
intermediate javascript exercise sols #29
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
Open
naab-c
wants to merge
19
commits into
rithmschool:master
Choose a base branch
from
naab-c:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e020f7b
function 1 sol
naab-c abfac49
function 1+2 sol
naab-c 5698a00
function 1+2+3+4 sol
naab-c c1b0bc7
function 1+2+3+4+5 sol
naab-c f6b21f4
recursion 1+2+3 sol
naab-c b4d5310
recursion 1+2+3+4 sol
naab-c a253fe5
recursion 1+2+3+4 sol simple and helper
naab-c 6bd6d7b
recursion 1+2+3+4+6
naab-c 26867d4
recursion 1+2+3+4+6 sol
naab-c 317c8c8
Merge branch 'master' of https://github.com/rithmschool/intermediate_…
naab-c 2cd69a8
loadash 1-15 sol
naab-c 38f8f15
canvas app sol
naab-c d7d53b6
comments fixed
naab-c 58f3863
sol 16-17 lodash
naab-c ab7f4a1
sol 16-17 lodash updated
naab-c a17aad4
call-apply-bind sol
naab-c 3fefe46
Merge branch 'master' of https://github.com/rithmschool/intermediate_…
naab-c c098e3d
prototype exercise sol
naab-c 3e0dffe
assesment hack-snooze partial sol
naab-c File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| function productOfArray(arr) { | ||
| if (arr.length === 1) { | ||
| return arr[0]; | ||
| } | ||
|
|
||
| return arr[0] * productOfArray(arr.slice(1)); | ||
| } | ||
|
|
||
| function collectStrings(obj) { | ||
| var nArr = []; | ||
| function helper(a) { | ||
| var kArr = Object.keys(a); | ||
| for (var i=0; i<kArr.length; i++) { | ||
| if (kArr.length === 0) | ||
| return; | ||
|
|
||
| if (typeof a[kArr[i]] === 'object') { | ||
| helper(a[kArr[i]]); | ||
| } | ||
| else | ||
| nArr.push(a[kArr[i]]); | ||
| } | ||
| } | ||
| helper(obj); | ||
| return nArr; | ||
| } | ||
|
|
||
| function contains(obj, num) { | ||
| var ret = false; | ||
| function helper(a) { | ||
| var kArr = Object.keys(a); | ||
|
|
||
| if (kArr.length === 0) | ||
| return; | ||
|
|
||
| for (var i=0; i<kArr.length; i++) { | ||
| if (typeof a[kArr[i]] === 'object') | ||
| helper(a[kArr[i]]); | ||
| else | ||
| if (a[kArr[i]] === num) | ||
| ret = true; | ||
| } | ||
| } | ||
| helper(obj); | ||
| return ret; | ||
| } | ||
|
|
||
|
|
||
| function search(arr, num) { | ||
| var idx = -1; | ||
| function helper(a, rId) { | ||
| if (rId === a.length) | ||
| return; | ||
|
|
||
| if(a[rId] === num) { | ||
| idx = rId; | ||
| } | ||
| else { | ||
| helper(a, ++rId) | ||
| } | ||
| } | ||
| helper(arr, 0); | ||
| return idx; | ||
| } | ||
|
|
||
| function binarySearch(arr, num) { | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| function replaceWith(entry, replaceChar, withChar) { | ||
| if (entry.indexOf(replaceChar) !== -1) { | ||
| for (var i=0; i < entry.length; i++) | ||
| if (entry[i] === replaceChar) | ||
| entry = entry.substr(0, i) + withChar + entry.substr(i + 1, entry.length - 1); | ||
| } | ||
|
|
||
| return entry; | ||
| } | ||
|
|
||
| function expand(arr, num) { | ||
| if (num <=0) | ||
| return arr; | ||
|
|
||
| var nArr = []; | ||
| for(var i=0; i < num; i++) | ||
| nArr = nArr.concat(arr); | ||
|
|
||
| return nArr; | ||
| } | ||
|
|
||
| function acceptNumbersOnly(...nums) { | ||
| for(var i=0; i < nums.length; i++) { | ||
| if (isNaN(nums[i])) | ||
| return false; | ||
| else if(typeof nums[i] !== 'number') | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| function mergeArrays(arr1, arr2) { | ||
| if (arr1.length === 0 && arr2.length === 0) | ||
| return []; | ||
|
|
||
| if(typeof(arr1[0]) === 'number' || typeof(arr2[0]) === 'number') | ||
| return arr1.concat(arr2).sort(function(a,b){return a - b}); | ||
| else | ||
| return arr1.concat(arr2).sort(); | ||
| } | ||
|
|
||
| function mergeObjects(obj1, obj2) { | ||
| for (var key in obj2) { | ||
| obj1[key] = obj2[key]; | ||
| } | ||
| return obj1; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,123 @@ | ||
| var expect = chai.expect; | ||
|
|
||
| // WRITE YOUR TESTS HERE! | ||
| // WRITE YOUR TESTS HERE! | ||
| describe("TESTS FOR replaceWith", function() { | ||
| it("happy path, character present twice", function() { | ||
| expect(replaceWith("awesome", "e", "z")).to.equal("awzsomz"); | ||
| }); | ||
|
|
||
| it("case sensitive single character", function() { | ||
| expect(replaceWith("Foo", "F", "B")).to.equal("Boo"); | ||
| }); | ||
|
|
||
| it("case sensitive multiple character", function() { | ||
| expect(replaceWith("FooF", "F", "B")).to.equal("BooB"); | ||
| }); | ||
|
|
||
| it("case sensitive single character with char present in lower and upper case", function() { | ||
| expect(replaceWith("Foof", "F", "B")).to.equal("Boof"); | ||
| }); | ||
|
|
||
| it("character not present", function() { | ||
| expect(replaceWith("awesome", "p", "z")).to.equal("awesome"); | ||
| }); | ||
|
|
||
| it("character not present case sensitive", function() { | ||
| expect(replaceWith("awesome", "E", "Z")).to.equal("awesome"); | ||
| }); | ||
|
|
||
| it("input string empty", function() { | ||
| expect(replaceWith("", "e", "z")).to.equal(""); | ||
| }); | ||
|
|
||
| it("replace with Char empty", function() { | ||
| expect(replaceWith("awesome", "", "z")).to.equal("awesome"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("TESTS FOR expand", function() { | ||
| it("happy path, expand integer array 3 times", function() { | ||
| expect(expand([1,2,3],3)).to.deep.equal([1,2,3,1,2,3,1,2,3]); | ||
| }); | ||
|
|
||
| it("happy path, expand string array 3 times", function() { | ||
| expect(expand(["foo", "test"],1)).to.deep.equal(["foo","test"]); | ||
| }); | ||
|
|
||
| it("expand array 0 times", function() { | ||
| expect(expand(["foo", "test"],0)).to.deep.equal(["foo","test"]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("TESTS FOR acceptNumbersOnly", function() { | ||
| it("number and string present", function() { | ||
| expect(acceptNumbersOnly(1,"foo")).to.be.false; | ||
| }); | ||
|
|
||
| it("numbers present only", function() { | ||
| expect(acceptNumbersOnly(1,2,3,4,5,6,7)).to.be.true; | ||
| }); | ||
|
|
||
| it("number and NaN present", function() { | ||
| expect(acceptNumbersOnly(1,2,3,4,5,6,NaN)).to.be.false; | ||
| }); | ||
| }); | ||
|
|
||
| describe("TESTS FOR mergeArrays", function() { | ||
| it("two number arrays", function() { | ||
| expect(mergeArrays([2,1],[3,4])).to.deep.equal([1,2,3,4]); | ||
| }); | ||
|
|
||
| it("two number arrays unequal length", function() { | ||
| expect(mergeArrays([2,1],[3,4,7])).to.deep.equal([1,2,3,4,7]); | ||
| }); | ||
|
|
||
| it("two string arrays unequal length", function() { | ||
| expect(mergeArrays(["a","c"],["b","d"])).to.deep.equal(["a","b","c","d"]); | ||
| }); | ||
|
|
||
| it("two string arrays unequal length", function() { | ||
| expect(mergeArrays(["a","c"],["f","b","d"])).to.deep.equal(["a","b","c","d","f"]); | ||
| }); | ||
|
|
||
| it("one empty array", function() { | ||
| expect(mergeArrays([],[3,4])).to.deep.equal([3,4]); | ||
| }); | ||
|
|
||
| it("two empty arrays", function() { | ||
| expect(mergeArrays([],[])).to.deep.equal([]); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| describe("TESTS FOR mergeObjects", function() { | ||
| it("two objects", function() { | ||
| var obj1 = {name: "Foo", num: 3}; | ||
|
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. careful with indentation here |
||
| var obj2 = {test: "thing",num: 55}; | ||
| var obj3 = {name: "Foo",test: "thing",num: 55}; | ||
| expect(mergeObjects(obj1,obj2)).to.deep.equal(obj3); | ||
| }); | ||
|
|
||
| it("one empty object", function() { | ||
| var obj1 = {}; | ||
| var obj2 = {test: "thing",num: 55}; | ||
| var obj3 = {test: "thing",num: 55}; | ||
| expect(mergeObjects(obj1,obj2)).to.deep.equal(obj3); | ||
| }); | ||
|
|
||
| it("both empty", function() { | ||
| var obj1 = {}; | ||
| var obj2 = {}; | ||
| var obj3 = {}; | ||
| expect(mergeObjects(obj1,obj2)).to.deep.equal(obj3); | ||
| }); | ||
|
|
||
| it("2 override, 1 extra field", function() { | ||
| var obj1 = {name: "Foo", num: 3}; | ||
| var obj2 = {name: "Bar", num: 55, field: "extra"}; | ||
| var obj3 = {name: "Bar",num: 55,field: "extra"}; | ||
| expect(mergeObjects(obj1,obj2)).to.deep.equal(obj3); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nice! small thing - do you need this outer if statement?
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.
agreed, have taken out the outer if statement.