Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
124 changes: 90 additions & 34 deletions lodash_exercise/lodash.js
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){
Copy link
Contributor

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!

return array.slice(1);
}
return array.slice(n);

}


function fromPairs(array) {
if(array.length === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better on the indentation front!

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();
Copy link
Contributor

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!

}

function take(array, num){
var newArr = [];
Copy link
Contributor

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!

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Watch your indentation - this stuff will drive developers crazy :)

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation and use !== in this case

}
} else if (typeof input === "string") {
return (input.indexOf(value) != -1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!= when we get to Python, !== in JavaScript

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for () here

} else {
return (Object.values(input).indexOf(value) > -1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for () here

}

}

function sample(arr){
return arr[Math.floor(Math.random() * (arr.length))];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for () around arr.length

}

function cloneDeep(){
Expand Down
52 changes: 52 additions & 0 deletions recursion_exercise/recursion.js
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Try to do this without using your collectStrings method :)

}




27 changes: 27 additions & 0 deletions testing_exercise/testing.js
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;
}
50 changes: 49 additions & 1 deletion testing_exercise/testingSpec.js
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(){
Copy link
Contributor

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.

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(){
Copy link
Contributor

Choose a reason for hiding this comment

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