Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
68 changes: 68 additions & 0 deletions recursion_exercise/recursion.js
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) {

}
48 changes: 48 additions & 0 deletions testing_exercise/testing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function replaceWith(entry, replaceChar, withChar) {
if (entry.indexOf(replaceChar) !== -1) {
Copy link
Contributor

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?

Copy link
Author

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.

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;
}
122 changes: 121 additions & 1 deletion testing_exercise/testingSpec.js
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};
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
});

});