Last active
February 21, 2017 02:24
-
-
Save camckin10/69cd3a133b1dfac3578dabc24347f6c4 to your computer and use it in GitHub Desktop.
Drills Answers
This file contains 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
EXAMPLE PROBLEM W/MICHAEL | |
//var number_list = [1,3,6,6,7,8,2] | |
function problem(number_list){ | |
//var number_list = [4,6,10,12] | |
var sum_counter = 0 | |
for(var i = 0; i <number_list.length; i++) { | |
var value = number_list[i] | |
sum_counter = value + sum_counter | |
} | |
return sum_counter; | |
} | |
----------------------------------------------------------- | |
1.)// Given a string, adds "-key" to it | |
// addKey('dog') => 'dog-key' | |
function addKey(string) { | |
return string + "-key"; | |
} | |
------------------------------------------------------------------------- | |
2.)// Changes the object to remove oldKey and add newKey with previous oldKey's value | |
// changeObjectKey({ dogs: 3, cats: 4 }, 'cats', 'lions') => { dogs: 3, lions: 4 } | |
function changeObjectKey(object, oldKey, newKey) { | |
object[newKey] = object[oldKey] | |
delete object[oldKey] | |
return object; | |
} | |
-------------------------------------------------------- | |
3.)// Add '-key' to a given key of the object | |
// addKeyToObjectKey({ dogs: 3, cats: 4 }, 'cats') => { dogs: 3, cats-key: 4 } | |
function addKeyToObjectKey(object, target_key) { | |
return changeObjectKey(object,target_key,addKey(target_key)) | |
} | |
function addKey(string) { | |
return string + "-key"; | |
} | |
function changeObjectKey(object, oldKey, newKey) { | |
object[newKey] = object[oldKey] | |
delete object[oldKey] | |
return object; | |
} | |
------------------------------------------------------ | |
4.)// Returns the number of keys present on the object | |
// howManyKeys({ dogs: 1 }) => 1 | |
// howManyKeys({ answerToTheUniverse: 42, everythingElse: 42 }) => 2 | |
function howManyKeys(object) { | |
var keyList = Object.keys(object) | |
return keyList.length | |
} | |
------------------------------------------------------------ | |
5.)// Increases the counter for every value in object | |
// increaseMultipleCounters({ answerToTheUniverse: 42, everythingElse: 42 }) => { answerToTheUniverse: 43, everythingElse: 43 } | |
function increaseMultipleCounters(object) { | |
var increaseResult = Object.keys(object) | |
for(var i = 0; i < increaseResult.length; i ++){ | |
value = increaseResult[i] | |
object[value] = object[value] + 1 | |
} | |
return object; | |
} | |
-------------------------------------------------------- | |
6.) //addKeyToMultipleObjectKeys({ dogs: 3, cats: 4 }) => { dogs-key: 3, cats-key: 4 } | |
function addKeyToMultipleObjectKeys(object) { | |
var key_list = Object.keys(object) | |
for(var i = 0; i < key_list.length; i ++){ | |
key = key_list[i] | |
changeObjectKey(object,key,addKey(key)) | |
} | |
return object; | |
} | |
function addKey(string){ | |
return string + "-key"; | |
} | |
function changeObjectKey(object,oldKey,newKey) { | |
object[newKey] = object[oldKey] | |
delete object[oldKey] | |
return object; | |
} | |
----------------------------------------------- | |
7.) | |
//CORRECT ANSWER | |
//1 is odd | |
//2 is even | |
function increaseMultipleCounters(object) { | |
var listOfKeys = Object.keys(object) | |
for(var i = 0; i < listOfKeys.length; i ++){ | |
key = listOfKeys[i] | |
if (object[key]%2 == 0){ | |
object[key] = object[key] + 2; | |
} | |
if (object[key]%2==1) { | |
object[key] = object[key]+1; | |
} | |
} | |
return object; | |
} | |
console.log(increaseMultipleCounters({dogs:2,cats:3})); | |
------------------------------------------------------------- | |
//SOLVE WITH SWITCH IN ANSWERS | |
//1=even | |
//2=odd | |
function increaseMultipleCounters(object) { | |
var listOfKeys = Object.keys(object) | |
for(var i = 0; i < listOfKeys.length; i ++){ | |
key = listOfKeys[i] | |
if (object[key]%2 == 0){ | |
object[key] = object[key] + 1; | |
} | |
if (object[key]%2==1) { | |
object[key] = object[key]+2; | |
} | |
} | |
return object; | |
} | |
-------------------------------------- | |
8.)//function objectFromPairs(pairs){ | |
//} | |
//Option A: .forEach method | |
//var buddies = [['hugs',4],['sun', 6]]; | |
function objectFromPairs(pairs){ | |
var object= {}; | |
pairs.forEach(function(element){ | |
object[element[0]]=element[1]; | |
}); | |
return object; | |
} | |
// option B: for loop | |
function objectPairs(pairs){ | |
var object = {}; | |
for(i = 0; i <pairs.length; i ++){ | |
element=pairs[i] | |
object[element[0]]= element[1]; | |
} | |
return object; | |
} | |
console.log("objectPairs([['hugs', 4], ['sun',8],[777,6668]])") | |
console.log(objectPairs([['hugs', 4], ['sun',8],[777,6668]])); | |
------------------------------------------ | |
9.)// Count how many times a given word appears on a list | |
// singleWordCounter(['apple', 'apple', 'dog', 'cat', 'orange', 'apple'], 'dog') => 1 | |
// singleWordCounter(['apple', 'apple', 'dog', 'cat', 'orange', 'apple'], 'apple') => 3 | |
var words = ['an','apple','went','shopping','an','cookie']; | |
function singleWordCounter(list,target_word){ | |
var count = 0 ; | |
for(var i = 0; i<list.length; i ++){ | |
if(list[i]===target_word){ | |
count ++ ; | |
} | |
} | |
return count; | |
} | |
singleWordCounter(words,'an'); | |
singleWordCounter(words,'apple'); | |
---------------------------------------------------------------------------------------- | |
10.) //Same as before, but returns an object with{count:number_of_occurrences} | |
function singleWordCounterAsObject(list,target_word){ | |
//var object={}; | |
//object['count']= 0; | |
var object = { 'count' : 0 } | |
for(var i = 0; i<list.length; i ++){ | |
if(list[i]===target_word){ | |
object['count'] ++ ; | |
} | |
} | |
return object; | |
} | |
console.log("singleWordCounterAO:") | |
console.log(singleWordCounterAsObject(['an','kiwi','went','shopping','an','cookie'], 'cat')) | |
console.log(singleWordCounterAsObject(words,'an')); | |
console.log(singleWordCounterAsObject(words,'kiwi')); | |
------------------------------------------------------------------------- | |
11.) //Same as before, but returns an object with {target_word:number_of_occurrences} | |
function singleWordCounterAsKeyObject(list, target_word){ | |
var object= {}; | |
object[target_word] = 0; | |
for(var i = 0;i <list.length; i ++){ | |
if(list[i]==target_word){ | |
object[target_word] ++; | |
} | |
} | |
if(object[target_word] > 1 ){ | |
object[target_word + "s"] = object[target_word] | |
delete object[target_word] | |
} | |
return object; | |
} | |
console.log("singleWordCounterAkO:") | |
console.log(singleWordCounterAsKeyObject(['an','kiwi','went','shopping','an','cookie'], 'cat')) | |
console.log(singleWordCounterAsKeyObject(words,'an')); | |
console.log(singleWordCounterAsKeyObject(words,'kiwi')); | |
------------------------------------------------------------------ | |
12.) //Same as before, but add an 's' to the key if the count is greater than 1 | |
function pluralSingleWordCounterAsKeyObject(list,target_word){ | |
var object= {}; | |
var mostFrequentWords = Object.keys(object) | |
object[target_word]= 0 ; | |
for(var i = 0; i <list.length; i ++){ | |
if(list[i]===target_word){ | |
return target_word + "-s"; | |
} if(list[i]) | |
} | |
} | |
//STILL INCOMPLETE? | |
---------------------------------------------------------------------- | |
//HARDER DRILLS | |
13.) //Replaces every value with "even" if it is even, and with "odd" otherwise | |
function increaseMultipleCounters(object) { | |
var listOfKeys = Object.keys(object) | |
for(var i = 0; i < listOfKeys.length; i ++){ | |
key = listOfKeys[i] | |
if (object[key]%2 == 0){ | |
object[key] = "even"; | |
} | |
if (object[key]%2==1) { | |
object[key] = "odd"; | |
} | |
} | |
return object; | |
} | |
console.log(increaseMultipleCounters({dogs:2,cats:3})); | |
-------------------------------------------------------------------------- | |
14.) // Returns the sum for every value from object | |
// sumValues({ dogs: 1, cats: 3, lions: 4 }) => 8 | |
var words = ['an','apple','went','shopping','an','cookie']; | |
function sumValues(object){ | |
var count = 0; | |
for(var i = 0;i<object.length;i ++){ | |
if(object[i])= target_word) { | |
count = count + [target_word]; | |
} | |
} | |
return count; | |
} | |
sumvalues(words,'an'); | |
--------------------------------------------------------------------- | |
TUTORING W/ MICHEAL | |
/ Count how many times a given word appears on a list | |
// singleWordCounter(['apple', 'apple', 'dog', 'cat', 'orange', 'apple'], 'dog') => 1 | |
// singleWordCounter(['apple', 'apple', 'dog', 'cat', 'orange', 'apple'], 'apple') => 3 | |
var words = ['an','apple','went','shopping','an','cookie']; | |
function singleWordCounter(list,target_word){ | |
var count = 0 ; | |
for(var ixx = 0; ixx<list.length; ixx ++){ | |
if(list[ixx]===target_word){ | |
count ++ ; | |
} | |
} | |
return count; | |
} | |
count = count + 1 | |
i = i+1 | |
count + object[target_word] | |
console.log("singleWordCounter:") | |
console.log(singleWordCounter(['an','apple','went','shopping','an','cookie'], 'cat')) | |
console.log(singleWordCounter(words,'an')); | |
console.log(singleWordCounter(words,'apple')); | |
// ----------------------------------------------------------------------------------- | |
function singleWordCounterAsObject(list,target_word){ | |
//var object={}; | |
//object['count']= 0; | |
var object = { 'count' : 0 } | |
for(var i = 0; i<list.length; i ++){ | |
if(list[i]===target_word){ | |
object['count'] ++ ; | |
} | |
} | |
return object; | |
} | |
console.log("singleWordCounterAO:") | |
console.log(singleWordCounterAsObject(['an','kiwi','went','shopping','an','cookie'], 'cat')) | |
console.log(singleWordCounterAsObject(words,'an')); | |
console.log(singleWordCounterAsObject(words,'kiwi')); | |
// ---------------------------------------------------------------------------- | |
//same, but returns an object with {target_word:number_of_occurrences} | |
function singleWordCounterAsKeyObject(list, target_word){ | |
var object= {}; | |
object[target_word] = 0; | |
for(var i = 0;i <list.length; i ++){ | |
if(list[i]==target_word){ | |
object[target_word] ++; | |
} | |
} | |
if(object[target_word] > 1 ){ | |
object[target_word + "s"] = object[target_word] | |
delete object[target_word] | |
} | |
return object; | |
} | |
console.log("singleWordCounterAkO:") | |
console.log(singleWordCounterAsKeyObject(['an','kiwi','went','shopping','an','cookie'], 'cat')) | |
console.log(singleWordCounterAsKeyObject(words,'an')); | |
console.log(singleWordCounterAsKeyObject(words,'kiwi')); | |
// //--------------------------------------------------- | |
// // 1/3 solution: function addKey(string) { | |
// // return string + "-key"; | |
// // } | |
// // if statement | |
// // for loop | |
// //if keys in object appear once, print key/value | |
// //if elements in array appear more than once, make plural, by adding -s, and print newkey | |
// function pluralSingleWordCounterAsKeyObject(list,target_word){ | |
// var object= {}; | |
// var mostFrequentWords = Object.keys(object) | |
// //object[target_word]= 0 ; | |
// for(var i = 0; i <list.length; i ++){ | |
// if(list[i]===target_word){ | |
// return target_word + "-s"; | |
// } if(list[i]) | |
// } | |
// } | |
----------------------------------------------- | |
WHAT SOLUTION IS THIS FOR:=?? | |
//function objectFromPairs(pairs){ | |
//} | |
//Option A: | |
//var buddies = [['hugs',4],['sun', 6]]; | |
function objectFromPairs(pairs){ | |
var object= {}; | |
pairs.forEach(function(element){ | |
object[element[0]]=element[1]; | |
}); | |
return object; | |
} | |
//for loop | |
function objectPairs(pairs){ | |
var object = {}; | |
for(i = 0; i <pairs.length; i ++){ | |
element=pairs[i] | |
object[element[0]]= element[1]; | |
} | |
return object; | |
} | |
console.log("objectPairs([['hugs', 4], ['sun',8],[777,6668]])") | |
console.log(objectPairs([['hugs', 4], ['sun',8],[777,6668]])); | |
HW--2/8/17 | |
var obj1 = { | |
name: 'john', | |
age: 32 | |
} | |
var obj2 = { | |
gender: 'male', | |
happy: true | |
} | |
function mergeObjects(obj1,obj2){ | |
Object.keys(obj1).forEach(function(key)){ | |
obj3[key] = obj1[key] | |
} | |
Object.keys(obj2).forEach(function(key)){ | |
obj3[key] = obj1[key] + obj2[key] | |
} | |
} | |
//can use Object.values for other problems | |
// key[value] | |
// object[key] | |
// obj3=object[key] | |
POSSIBLE ANSWER? | |
14.) // Returns the sum for every value from object | |
// sumValues({ dogs: 1, cats: 3, lions: 4 }) => 8 | |
var words = ['an','apple','went','shopping','an','cookie']; | |
function sumValues(object){ | |
var count = 0; | |
for(var i = 0;i<object.length;i ++){ | |
if(object[i])= target_word) { | |
count = count + [target_word]; | |
} | |
} | |
return count; | |
} | |
sumvalues(words,'an'); | |
Object.values() | |
---------------------------------------- | |
NOTES WITH MICHAEL 2-10-2017 | |
//.split method example | |
"xx;yy;zz.q".split(/;/) | |
return ["xx","yy","zz.q"] | |
"xx;yy;zz.q".split(/;+/) | |
return ["xx","yy","zz.q"] | |
"xx;;yy;zz.q".split(/;/) | |
["xx", "","yy","zz.q"] | |
"xx;;yy;zz.q".split(/;+/) | |
["xx", "yy", "zz.q"] | |
Regular expression special characters | |
+ = 1 or more | |
- //does not exist;regular character | |
* = 0 or more | |
? = 0 or 1 | |
.split method example | |
"x,y,z".split(':') | |
["x,y,z"]--returns array exactly like it is | |
"x:y,z".split(":")---return | |
["x"],["y","z"]//incorrect | |
["x","y,z"] | |
["x","y","z"]//incorrect | |
["xy","z"]//incorrect | |
"x:y,z".split(",")---return | |
["x:y","z"] | |
"xx;yy;zz.q".split(";") | |
["xx","yy","zz.q"] | |
"xx;;yy;zz.q".split(";") | |
["xx","","yy","zz.q"] | |
split takes this expression and splits array based on character in | |
parenthesis | |
if character in parenthesis is not in original array, then array will return | |
original array | |
--------- | |
Challenge Question Answer | |
function getTokens(rawString) { | |
// NB: `.filter(Boolean)` removes any falsy items from an array | |
return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort(); | |
} | |
/returning a lowercase string which will be | |
/.split = split takes this expression and splits array based on character in | |
parenthesis | |
if character in parenthesis is not in original array, then array will return | |
original array | |
/[regular expression]/ | |
/.filter(function) = scan the array, call function on each element in array and remove element where function returns false | |
/.sort = sorts arrays in place and returns elements in alphabetical order | |
//mostFrequentWord is an object--first object | |
//the object has a for loop loops over words(array) to build another object | |
//when looping over array, if word is already in object, increment word by 1 | |
//if its not in the object it will add it to the obj | |
// 2nd object keeping in count of frequency of words (currentMaxCount) | |
//loops over frequency(mostFrequentWord) object to figure out which one of words appears the mos t frequently | |
//returns word that appears the most (currentMaxKey) | |
function mostFrequentWord(text) { | |
var words = getTokens(text);//array that has been sorted | |
// words= array of strings | |
var wordFrequencies = {};//empty object | |
for (var i = 0; i <= words.length; i++) {//for loop | |
if (words[i] in wordFrequencies) { //array in empty object? | |
wordFrequencies[words[i]]++; | |
**//string is indexing in the ith position; will increase by 1 a certain | |
//amount of times depending on array length. | |
// | |
} | |
else { | |
wordFrequencies[words[i]]=1; | |
//words[i] is in the ith position; set the value associated with the key to 1 | |
} | |
} | |
var currentMaxKey = Object.keys(wordFrequencies)[0]; | |
**//currentMaxKey = all of keys of object wordFrequencies has value of 0 | |
//currentMaxKey becomes a new object? | |
var currentMaxCount = wordFrequencies[currentMaxKey]; | |
**//currentMaxCount = adding currentMaxKey object to wordFrequencies object | |
//using bracket notation ; create new object currentMaxCount to hold all | |
//key/value pairs from previous 2 objects | |
for (var word in wordFrequencies) | |
if (wordFrequencies[word] > currentMaxCount) { | |
//if object[with newly added array] is greater than currentMaxCount, | |
currentMaxKey = word; | |
//currentMaxKey is equal to word array | |
currentMaxCount = wordFrequencies[word]; | |
//object is equal to object[newly added array] | |
} | |
} | |
return currentMaxKey; | |
**//return new object with updated keys/values | |
} | |
//OBJECT DRILLS 2 SOLUTIONS | |
//1.) makeStudentsReport | |
function makeStudentsReport(data) {//function name w/ argument | |
var answers = [];//create an empty array | |
for (var i=0; i<data.length; i++) {//for loop that measures length | |
var reports = data[i];//create another variable that has the argument | |
//inputting the for loop variable? | |
answers.length(reports.name + reports.grade);//ask the computer to | |
//print the length of the answers | |
} | |
return answers;//returning the array with proper information | |
} | |
console.log['Sarah', 'C']; | |
console.log['David', 'A']; | |
//2.) enroll in Summer School | |
//factory function solution option | |
function enrollInSummerSchool (students) { | |
var student1= { | |
name:'sally', | |
status: 'new student', | |
course: 'history' | |
}, | |
var student2 = { | |
name:'david', | |
status: 'withdrawn', | |
course: 'math' | |
} | |
}; | |
function SummerSchool (students) { | |
return { | |
name: 'sally', | |
status: 'in summer school', | |
course: 'history' | |
}; | |
return { | |
name: 'david', | |
status: 'in summer school', | |
course: 'math' | |
} | |
}; | |
//3.) Find by id problem | |
//4.) validateKeys problem | |
//function called validateKeys | |
//function validateKeys takes two arguments, object & expectedKeys | |
//argument object is an object to validate keys | |
//argument expectedKeys is an array of keys found in object | |
//return true if object has all of the keys from | |
// expectedKeys AND no additional keys | |
// return false if 1 or more key is missing from object OR if object | |
// has extra keys not in expectedKeys | |
//SOLUTION SHOULD RETURN BOOLEAN VALUE | |
//QUESTION:when a problem mentions 'if' does that mean you HAVE to | |
//use an if, if/else, or else/if statement? | |
var expectedKeys = ['name','age','eyeColor','hometown']; | |
function validateKeys(object,expectedKeys) { | |
if (object1 == expectedKeys){ | |
return true; | |
}else(object1.keys > expectedKeys){ | |
return false; | |
} if (object2.keys < expectedKeys){ | |
return false; | |
}else(object2.keys > expectedKeys){ | |
return false; | |
} | |
} | |
//objects to test when ready to run problem | |
var object1 = { | |
name: 'sally', | |
age: '34', | |
eyeColor: 'brown', | |
hometown: 'miami' | |
} | |
var object2 = { | |
name: 'dave', | |
age: '12', | |
hometown: 'minneapolis' | |
} | |
// //possible if statements to solve problem | |
// if (Object.keys === expectedKeys){ | |
// return true | |
// } if (expectedKeys){ | |
// return true | |
// }else (Object.keys <(less than) expectedKeys){ | |
// return false | |
// } else (expectedKeys >(more than) Object.Keys){ | |
// return false | |
// } | |
//maybe this too? ---> var i = 0 ; i< expectedKeys.length; i ++ ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment