Last active
May 4, 2017 14:07
-
-
Save iancover/e9544497ccf2237c765a4c33aaf1b45f to your computer and use it in GitHub Desktop.
Object Drills 2
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
// Write a function called enrollInSummerSchool that takes a single argument, students. | |
// students is an array of objects, with each object representing a student — | |
// for example, {name: 'Tim', status: 'Current student', course: 'Biology'}. | |
// enrollInSummerSchool should return an array of objects. | |
// For each object from the original array, it should return the original name and course, | |
// but should update the status to In Summer school. | |
function enrollInSummerSchool(students) { | |
var results = []; | |
for (var i=0; i<students.length; i++) { | |
var student = students[i]; | |
results.push({ | |
name: students[i].name, | |
status: 'In Summer school', | |
course: students[i].course | |
}); | |
} | |
return results; | |
} |
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
// Write a function called findById. This function takes two arguments 'items' and 'idNum'. | |
// 'items' is an array of objects. idNum is the id we're trying to find in items. | |
// Calling findById([{id: 1, foo: 'bar'}, {id: 2, foo: 'bizz'}], 2) should return {id: 2, foo: 'bizz'}. | |
function findById(items, idNum) { | |
for (var i=0; i<items.length; i++) { | |
if (idNum === items[i].id) { | |
return items[i]; | |
} | |
} | |
} |
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
// Make Student Reports | |
// Write a function called 'makeStudentReports' that takes a single argument 'data' ('data' is an array of objects). | |
// Each object in the array represents a student and their letter grade for each example {name:'John', grade: 'A'} | |
// makeStudentReports should return an array of strings for each item in data return a string that looks like: | |
// '[name]:[grade]' - the name and grade values should be substituted in. | |
function makeStudentsReport(data) { | |
var results = []; | |
for (var i=0; i<data.length; i++) { | |
var item = data[i]; | |
results.push(item.name + ': ' + item.grade); | |
} | |
return results; | |
} | |
// Solution using .map method | |
// below produces the same result as ^^, but with `.map` | |
function makeStudentsReportAlt(data) { | |
return data.map(function(d) { | |
return d.name + ': ' + d.grade; | |
}); | |
} | |
// *** HOW DOES THE FUNCTION KNOW THAT d = the item on the array? |
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
// Write a function called 'makeToDos'. This function should take two arguments: 'owner' and 'toDos'. | |
// 'owner' is a string of the name of the owner of the to do list. | |
// 'toDos' is an array of strings representing items to be done. | |
// 'makeToDos' should return an object with 'owner' and 'toDos' properties that correspond to the values passed in as arguments. | |
// It should also have a method called .generateHtml. This method should return a string representing an unordered list. | |
// Each item from toDos should appear as an <li> in the list. | |
// For example: if 'makeToDos' was called like this: | |
// makeToDos('Teddy', ['wake', 'eat', 'drink', 'work', 'sleep']), | |
// calling .generateHtml on the resulting object should generate a string like this: | |
// '<ul><li>wake</li><li>eat</li><li>drink</li><li>work</li><li>sleep</li></ul>'. | |
// Couldn't figure out how to do it | |
// Curriculum solution: | |
// owner - string meaning the name of the owner | |
// toDos - ARRAY of STRINGS representing ITEMS to be done | |
// makeToDos - should RETURN an OBJECT with 'owner' and 'toDos' properties that | |
// correspond to values passed as arguments | |
// Should have a method called '.generateHtml' which should return a string | |
// representing an UNORDERED LIST, each ITEM from 'toDos' should appear as a <li> | |
// in the list | |
function makeToDos(owner, toDos) { | |
return { | |
owner: owner, | |
toDos: toDos, | |
generateHtml: function() { | |
var html = '<ul>'; | |
this.toDos.forEach(function(todo) { | |
html+= '<li>' + todo + '</li>'; | |
}); | |
return html + '</ul>'; | |
} | |
} | |
} |
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
// Write a function called validateKeys. This function takes two arguments, 'object' and 'expectedKeys'. | |
// 'object' is an object that we want to valdiate keys for. 'expectedKeys' is an array of keys that we EXPECT TO FIND | |
// on the object. | |
// validateKeys should return false if 'object' contains extra keys not in 'expectedKeys' or missing keys | |
// validateKeys should return false if 'object' contains different keys than in 'expectedKeys' | |
// ...therefore, | |
// validateKeys should return true if object has all of the keys from expectedKeys and no additional keys | |
// running the function with `objectA` and `expectedKeys` | |
// should return `true` | |
var objectA = { | |
id: 2, | |
name: 'Jane Doe', | |
age: 34, | |
city: 'Chicago' | |
} | |
// running the function with `objectA` and `expectedKeys` | |
// should return `false` | |
var objectB = { | |
id: 3, | |
age: 33, | |
city: 'Peoria' | |
} | |
// ********** MY SOLUTION HERE ***************** | |
// object = argument thats an object | |
// expectedKeys = argument thats an array of keys | |
var expectedKeys = [ | |
'id', 'name', 'age', 'city' | |
]; | |
function validateKeys(object, expectedKeys) { | |
if (Object.keys(object).length !== expectedKeys.length) { | |
return false; | |
} | |
for (var i=0; i<expectedKeys; i++) { | |
if (Object.keys(object) !== expectedKeys[i]); | |
return false; | |
} | |
return true; | |
} | |
// For some reason I got it to work with 2nd argument in loop logic ' i<expectedKeys ' and doesn't | |
// work with ' i<expectedKeys.length ' so now Im more confused | |
// Remember to use logic operators: ! , !== , === | |
// 1. First return the 'false' statements by disqualifying | |
// if arg1.length !== arg2.length return false | |
// | |
// 2. Validate if keys in arg1 (the object) match keys (or items in array) in arg2 | |
// keys in arg1 !== items in arg2 - return false | |
// | |
// a. for..in loop iterating expectedKeys | |
// b. if arg1 content doesn't match arg2 content | |
// | |
// 3. If non of the above then its true | |
// return true; | |
// Curriculum Solution uses the .find() method | |
function validateKeys(object, expectedKeys) { | |
if (Object.keys(object).length !== expectedKeys.length) { | |
return false; | |
} | |
for (var i; i<expectedKeys.length; i++) { | |
if (!Object.keys(object).find(function(key) { | |
return key === expectedKeys[i]; | |
})) { | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment