Created
August 12, 2018 14:49
-
-
Save DynamiteC/52d63be49a6dca6c74ea74b82defc225 to your computer and use it in GitHub Desktop.
Thinkful_Object_Drills_II
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
| var studentData = [ | |
| { | |
| name: 'Tim', | |
| status: 'Current student', | |
| course: 'Biology' | |
| }, | |
| { | |
| name: 'Sue', | |
| status: 'Withdrawn', | |
| course: 'Mathematics' | |
| }, | |
| { | |
| name: 'Liz', | |
| status: 'On leave', | |
| course: 'Computer science' | |
| } | |
| ]; | |
| function enrollInSummerSchool(students) { | |
| // your code here | |
| for(var x = 0; x < students.length; x++) | |
| students[x].status = 'In Summer school'; | |
| return students | |
| } | |
| /* From here down, you are not expected to understand.... for now :) | |
| Nothing to see here! | |
| */ | |
| // tests | |
| function testIt() { | |
| var testData = [ | |
| { | |
| name: 'Burt', | |
| status: 'Playing hooky', | |
| course: 'Biology' | |
| }, | |
| { | |
| name: 'Melanie', | |
| status: 'Sick', | |
| course: 'Mathematics' | |
| }, | |
| { | |
| name: 'Leonard', | |
| status: 'AWOL', | |
| course: 'Computer science' | |
| } | |
| ]; | |
| var results = enrollInSummerSchool(testData); | |
| if (!(results && results instanceof Array)) { | |
| console.error('FAILURE: `enrollSummerSchool` must return an array'); | |
| return | |
| } | |
| for (var i=0; i<testData.length; i++) { | |
| var result = results.find(function(_result) { | |
| return ( | |
| _result.name === testData[i].name && | |
| _result.course === testData[i].course && | |
| _result.status === 'In Summer school'); | |
| }); | |
| if (!result) { | |
| console.error( | |
| 'FAILURE: `enrollSummerSchool` should return ' + | |
| 'original key/value pairs for each student, and ' + | |
| 'update `status` to "In Summer school"'); | |
| return | |
| } | |
| } | |
| console.info('SUCCESS: `enrollSummerSchool` is working'); | |
| } | |
| testIt(); |
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
| // you can pass in `scratchData` to test out `findByid` | |
| // your function | |
| var scratchData = [ | |
| {id: 22, foo: 'bar'}, | |
| {id: 28, foo: 'bizz'}, | |
| {id: 19, foo: 'bazz'} | |
| ]; | |
| function findById(items, idNum) { | |
| // your code here | |
| for(var x = 0; x < items.length; x++) | |
| { | |
| if(items[x].id == idNum) | |
| return items[x]; | |
| } | |
| } | |
| // | |
| function testIt() { | |
| var testData = [ | |
| {id: 1, foo: 'bar'}, | |
| {id: 2, foo: 'bizz'}, | |
| {id: 3, bang: 'boo'} | |
| ]; | |
| var result = findById(testData, 3); | |
| if (!(result && result !== null && typeof result === 'object')) { | |
| console.error('`findById` must return an object'); | |
| return | |
| } | |
| if (result.id !== 3) { | |
| console.error( | |
| 'Asked for item with id of `3` but got back one with id of ' + | |
| result.id | |
| ); | |
| return | |
| } | |
| if (result.bang !== 'boo') { | |
| console.error('Expected all key/value pairs from target object to be returned'); | |
| return | |
| } | |
| console.log('SUCCESS: `findByid` is working'); | |
| } | |
| testIt(); |
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
| function makeStudentsReport(data) { | |
| // your code here | |
| var arr = []; | |
| for(var x = 0; x < data.length; x++) | |
| { | |
| arr.push(data[x].name + ': ' + data[x].grade); | |
| } | |
| return arr; | |
| } | |
| /* From here down, you are not expected to | |
| understand.... for now :) | |
| Nothing to see here! | |
| */ | |
| // tests | |
| function testIt() { | |
| var testData = [ | |
| {name: 'Jane Doe', grade: 'A'}, | |
| {name: 'John Dough', grade: 'B'}, | |
| {name: 'Jill Do', grade: 'A'} | |
| ]; | |
| var expectations = [ | |
| 'Jane Doe: A', | |
| 'John Dough: B', | |
| 'Jill Do: A' | |
| ]; | |
| var results = makeStudentsReport(testData); | |
| if (!(results && results instanceof Array)) { | |
| console.error( | |
| 'FAILURE: `makeStudentsReport` must return an array'); | |
| return | |
| } | |
| if (results.length !== testData.length) { | |
| console.error( | |
| 'FAILURE: test data had length of ' + testData.length + | |
| ' but `makeStudentsReport` returned array of length ' + results.length); | |
| return | |
| } | |
| for (var i=0; i < expectations.length; i++) { | |
| var expect = expectations[i]; | |
| if (!results.find(function(item) { | |
| return item === expect; | |
| })) { | |
| console.error( | |
| 'FAILURE: `makeStudentsReport` is not ' + | |
| 'producing expected strings' | |
| ); | |
| return | |
| } | |
| } | |
| console.log('SUCCESS: `makeStudentsReport` is working'); | |
| } | |
| testIt(); |
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
| function makeToDos(owner, toDos) { | |
| // your code here | |
| var obj = {owner:owner,toDos:toDos,generateHtml: function() { | |
| var html = '<ul>'; | |
| this.toDos.forEach(function(todo) { | |
| html+= '<li>' + todo + '</li>'; | |
| }); | |
| return html + '</ul>'; | |
| } | |
| } | |
| return obj | |
| } | |
| /* From here down, you are not expected to | |
| understand.... for now :) | |
| Nothing to see here! | |
| */ | |
| // tests | |
| function testIt() { | |
| var toDos = ['get milk', 'walk dog', 'pay bills', 'eat dinner']; | |
| var owner = 'Steve'; | |
| var myToDos = makeToDos(owner, toDos); | |
| if (!myToDos || !myToDos instanceof Object) { | |
| console.error('FAILURE: `makeToDos` must return an object'); | |
| return; | |
| } | |
| var expectedKeys = ['owner', 'toDos', 'generateHtml']; | |
| var missingKeys = expectedKeys.filter(function(key) { | |
| return Object.keys(myToDos).indexOf(key) < 0; | |
| }); | |
| if (missingKeys.length > 0) | |
| { | |
| console.error( | |
| 'FAILURE: `makeToDos` missing following keys: ' + missingKeys.join(', ')); | |
| return; | |
| } | |
| if (myToDos.owner !== owner) { | |
| console.error( | |
| 'FAILURE: expected `makeToDos` to return an object with `.owner` '+ | |
| 'set to value passed in for `owner`, in this case ' + owner); | |
| return; | |
| } | |
| if (!toDos.every(function(toDo) { | |
| return myToDos.toDos.find(function(_toDo) { | |
| return _toDo === toDo; | |
| }) | |
| })) { | |
| console.error('FAILURE: makeToDos toDos property returned' + Object.values(myToDos.toDos) + '. Expected: ' + Object.values(todos)); | |
| } | |
| var element = $(myToDos.generateHtml()); | |
| if (element.length !== 1) { | |
| console.error( | |
| 'FAILURE: `makeToDos` must return an object with a `generateHtml` ' + | |
| 'method that returns an unordered list'); | |
| return; | |
| } | |
| if (!toDos.every(function(toDo) { | |
| return element.find('li:contains("' + toDo + '")').length === 1; | |
| })) { | |
| console.error('FAILURE: generateHtml must return li element for every todo'); | |
| return | |
| } | |
| console.log('SUCCESS: `makeToDos` is working'); | |
| } | |
| testIt(); |
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
| // 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' | |
| } | |
| var expectedKeys = [ | |
| 'id', 'name', 'age', 'city' | |
| ]; | |
| function validateKeys(object, expectedKeys) { | |
| // your code here | |
| var keys = Object.keys(object); | |
| if(keys.length!=expectedKeys.length) | |
| return false; | |
| for(var x = 0; x < keys.length; x++) | |
| { | |
| var flag = false; | |
| for(var y = 0; y < expectedKeys.length; y++) | |
| { | |
| if(expectedKeys[y]==keys[x]) | |
| { | |
| flag = true; | |
| break; | |
| } | |
| } | |
| if(!flag) | |
| return false; | |
| } | |
| return true; | |
| } | |
| /* From here down, you are not expected to | |
| understand.... for now :) | |
| Nothing to see here! | |
| */ | |
| function testIt() { | |
| var objectA = { | |
| id: 2, | |
| name: 'Jane Doe', | |
| age: 34, | |
| city: 'Chicago' | |
| } | |
| var objectB = { | |
| id: 3, | |
| age: 33, | |
| city: 'Peoria' | |
| } | |
| var objectC = { | |
| id: 9, | |
| name: 'Billy Bear', | |
| age: 62, | |
| city: 'Milwaukee', | |
| status: 'paused' | |
| } | |
| var expectedKeys = [ | |
| 'id', 'name', 'age', 'city' | |
| ]; | |
| if (typeof validateKeys(objectA, expectedKeys) !== 'boolean') { | |
| console.error( | |
| 'FAILURE: `validateKeys` should return a boolean value'); | |
| return; | |
| } | |
| if (!validateKeys(objectA, expectedKeys)) { | |
| console.error( | |
| 'FAILURE: running `validateKeys` with the following object and keys ' + | |
| 'should return `true` but returned `false`:\n' + | |
| objectA + '\n' + expectedKeys | |
| ) | |
| return; | |
| } | |
| if (validateKeys(objectB, expectedKeys)) { | |
| console.error( | |
| 'FAILURE: running `validateKeys` with the following object and keys ' + | |
| 'should return `false` but returned `true`:\n' + | |
| objectB + '\n' + expectedKeys | |
| ); | |
| } | |
| if (validateKeys(objectC, expectedKeys)) { | |
| console.error( | |
| 'FAILURE: running `validateKeys` with the following object and keys ' + | |
| 'should return `false` but returned `true`:\n' + | |
| objectC + '\n' + expectedKeys | |
| ); | |
| } | |
| console.log('SUCCESS: `validateKeys` is working'); | |
| } | |
| testIt() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment