Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
51 changes: 44 additions & 7 deletions lab/lab1/part1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,25 @@
Instructions: "Write a function that adds one to the number provided"
Example: "plusOne(2) should return 3"
===================== */
/* =====================
var f = function(num1) {
var ff= function (num2){
return num1+num2
}
}
f(1)(2)
===================== */




var plusOne = function() {};


var plusOne = function(num) {
if(typeof num ==='number'){
return num = num + 1
}
};

console.log('plusOne success:', plusOne(99) === 100);

Expand All @@ -17,7 +34,11 @@ Instructions: "Write a function, age, that takes a birth year and returns an age
Example: "age(2000) should return 18"
===================== */

var age = function(birth) {};
var age = function(birth) {
if (typeof birth === 'number') {
return birth = 2017-birth
}
};

console.log('age success:', age(1971) === 46);

Expand All @@ -26,7 +47,13 @@ Instructions: "Write a function that returns true for numbers over 9000 and fals
Example: "over9000(22) should return false"
===================== */

var over9000 = function() {};
var over9000 = function(num) {
if (num > 9000){
return true
} else{
return false
}
};

console.log('over9000 success:', over9000(9001) === true && over9000(12) === false);

Expand All @@ -35,7 +62,9 @@ Instructions: "Write a function that returns the value of an object at a specifi
Example: "valueAtKey({'name': 'Nathan'}, 'name') should return 'Nathan'"
===================== */

var valueAtKey = function() {};
var valueAtKey = function(key,name) {
return key[name]
};

console.log('valueAtKey success:', valueAtKey({'foo': 'bar'}, 'foo') === 'bar');

Expand All @@ -45,7 +74,9 @@ Example: "y(0, 0, 0) should return 0; y(1, 1, 1) should return 2"
Remember: The standard mathematical expression for such a function is y=mx+b
===================== */

var y = function() {};
var y = function(a,b,c) {
return a*b+c
};

console.log('y success:', y(12, 1, 12) === 24);

Expand All @@ -54,7 +85,13 @@ Instructions: "Write a function which counts the number of times a value occurs
Example: "countItem(['a', 'b', 'a'], 'a') should return 2"
===================== */

var countItem = function() {};
var countItem = function(num,x) {
count = 0
for (var i = 0; i < num.length; i++) {
if (num[i] === x){
count = count + 1
}
} return count
};

console.log('countItem success:', countItem([1, 2, 3, 4, 5, 4, 4], 4) === 3);

72 changes: 63 additions & 9 deletions lab/lab1/part2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ Functions that `return` can be passed as values to other functions. Each exercis
Instructions: Write a function that *always* returns the number 1.
===================== */

var justOne = function() {};
var justOne = function() {
return 1
};

console.log('justOne success:', justOne() === 1);

/* =====================
Instructions: Write a function that returns true if a number is even.
===================== */

var isEven = function() {};
var isEven = function(num) {
if (num%2 === 0){
return true
}else {
return false
}
};

console.log('isEven success:', isEven(2) === true && isEven(3) === false);

Expand All @@ -24,15 +32,21 @@ Instructions: Write a function that *always* returns false.
Use functions "justOne" and "isEven" somehow in the definition.
===================== */

var justFalse = function() {};
var justFalse = function(fal) {
return !justOne
};

console.log('justFalse success:', justFalse() === false);

/* =====================
Instructions: Write a function that takes a boolean value and returns its opposite.
===================== */

var not = function() {};
var not = function(boolean) {
if (true) {return false
} else { return true
}
};

console.log('not success:', not(true) === false);

Expand All @@ -41,19 +55,52 @@ Instructions: Write a function that returns true if a number is odd
Use functions "isEven" and "not" somehow in the definition.
===================== */

var isOdd = function() {};
var isOdd = function(num) {
return !(isEven(num));
};

console.log('isOdd success:', isOdd(4) === false);

/* =====================
Instructions: Write a function that takes a list of numbers and returns a list with only numbers above 10
===================== */

var filterOutLessThan10 = function() {};
var filterOutLessThan10 = function(list) {
holder = []
for (var i = 0; i < num.length; i++){
if(list[i] > 10){
holder.push(list[i])
}
} return holder
};

// The function 'arraysEqual' (which it is your task to define) is necessary because
// ([4] === [4]) is *false* in javascript(!!!)
// Use google + stackoverflow to figure out how to define a function which returns true given two equal arrays
function arraysEqual(arr1, arr2) { return false; }
function arraysEqual1(arr1, arr2) {
if (arr1 === arr2) {
return true;} else if (arr1 == null || arr2 == null) {
return false;} else if (arr1.length != arr2.length) {
return false;}
else{
for (var i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
}
return true;
}

function arraysEqual(arr1){
hold=[];
for (var i = 0; i < arr1.length; i++){
if(arr1[i] > 10) {
hold.push(arr1[i])
}
} return hold;
}

console.log('filterOutLessThan10 success:', arraysEqual([4, 11], [11]));

/* =====================
Expand All @@ -63,7 +110,14 @@ Instructions: Let's bring it all together. Write a function that filters a list
2. a function that takes a value and returns true (to keep a number) or false (to toss it out)
===================== */

var filter = function(array, func) {};
var filter = function(array, func) {
var hold=[];
for (var i = 0; i < array.length; i++){
if (func(array[i]) == true){
hold.push(array[i]);
}
}
return hold
};

console.log('filter success:', filter([4, 11], isOdd) === [11]);

16 changes: 8 additions & 8 deletions lab/lab2/part1.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,62 @@ console.log('Nathan\'s list', nathanGameList);
What is the first game in Jeff's list?
===================== */

var query1;
var query1 = _.first(jeffGameList);

console.log('What is the first game in Jeff\'s list?', query1);

/* =====================
What are all of the games except for the first game in Jeff's list?
===================== */

var query2;
var query2 = _.rest(jeffGameList);

console.log('What are all of the games except for the first game in Jeff\'s list?', query2);

/* =====================
What is the last game in Nathan's list?
===================== */

var query3;
var query3 = _.last(nathanGameList);

console.log('What is the last game in Nathan\'s list?', query3);

/* =====================
What are all of the games in Nathan's list except for the last?
===================== */

var query4;
var query4 = _.initial(nathanGameList);

console.log('What are all of the games in Nathan\'s list except for the last?', query4);

/* =====================
What would Nathan's game list look like if he sold "catan"?
===================== */

var query5;
var query5 = _.without(nathanGameList,"catan");

console.log('What would Nathan\'s game list look like if he sold "catan"?', query5);

/* =====================
If Nathan and Jeff play a board game, what are their options? This should be a list of all games owned by Jeff or Nathan, with no duplicates.
===================== */

var query6;
var query6 = _.union(jeffGameList,nathanGameList);

console.log('If Nathan and Jeff play a board game, what are their options? This should be a list of all games owned by Jeff or Nathan, with no duplicates.', query6);

/* =====================
Which games are owned by both Jeff and Nathan?
===================== */

var query7;
var query7 = _.intersection(jeffGameList,nathanGameList);

console.log('Which games are owned by both Jeff and Nathan', query7);

/* =====================
Which games are exclusive to collections? In other words, only owned by either Jeff or Nathan.
===================== */

var query8;
var query8 = _.difference(jeffGameList,nathanGameList);

console.log('Which games are exclusive to one collection? In other words, only owned by either Jeff or Nathan (but not both!).', query8);
8 changes: 6 additions & 2 deletions lab/lab2/part2.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ Calculate the value by using _.countBy and set your answer to variable "largeSta
===================== */

var data = bikeArrayClean;
var largeStationList=_.filter(data,function(array){return array[3]>20;});
console.log(largeStationList)

var largeStationList;

var largeStationCount;
var largeStationCount = _.countBy(data,function(num){
return num[3]>20? 'largeStation':'smallStation';
});
console.log(largeStationCount)
Loading