Skip to content

Instantly share code, notes, and snippets.

@camckin10
Last active March 15, 2017 01:09
Show Gist options
  • Select an option

  • Save camckin10/8b500dbbd2e16a878a38210a362276bf to your computer and use it in GitHub Desktop.

Select an option

Save camckin10/8b500dbbd2e16a878a38210a362276bf to your computer and use it in GitHub Desktop.
objectdrills2.js
1.)make student report
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']
//only prints the letter grade, and not name:grade
//work with pedro
CORRECT ANSWER:
1.)make student report
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.push( '[' + reports.name + ']' + ': ' + '[' + reports.grade + ']');
}
return answers;//returning the array with proper information
}
//console.log('Sarah', 'C');
console.log (makeStudentsReport([ {name: 'Billy Boy', grade: 'D'},
{name: 'Serious Sara', grade: 'B'},
{name: 'Tepid Tom', grade: 'C'} ]));
2.) enroll in summer school
//factory function solution option
//should return status: in summer school
//solution a parameterized factory function?
WHAT I HAVE SO FAR : (ASSISTANCE FROM PEDRO)
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) {
students = [];
studentData.forEach(function(obj) {
Object.values(obj).forEach(function(value) {
studentData.status = 'In Summer School';
students.status = studentData.status
//console.log(studentData);
});
});
enrollInSummerSchool();
}
//function enrollInSummerSchool(students)
//studentData.forEach(function(obj){
//Object.values(obj).forEach(function(value) {
//studentData['status'] = 'In Summer School';
//console.log(studentData);
//}
//}
//}
/* 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();
3.) find id problem
SOLUTION 3/7/17 CODE WORKED :)
// 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'}
];
//Return the id key, which value is a number
function findById(items, idNum) {
scratchData.forEach(function(obj){
console.log(obj.id);
})
}
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?
//possibly use try catch finally blocks to solve problem
SOLUTION 3/7/17 CODE WORKED! :)
// 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) {
if (objectA.keys != expectedKeys.length){
return true;
} else (objectB.keys > expectedKeys.length)
return false;
}
console.log(validateKeys(objectA,expectedKeys));
console.log(validateKeys(objectB,expectedKeys));
//5.) to do list --factory functions problem
WORK WITH PEDRO 3/10/17
1.) //find id number
// 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'}
];
//Return the id key, which value is a number
function findById(items, idNum) {
for(var i = 0; i < items.length; i ++ ){
if(items[i].id == idNum) {
return items[i];
}
//console.log(obj.id);
}
}
//if(items[i].id == idNum) { returns items[i] }
console.log (findById(scratchData, 28));
// if (obj == idNum){
// return 'id' + ':' + 'foo' + ':' + 'bar';
// }
//if(obj.id == idNum) { return obj }
2.) //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) {
if (objectA.keys == expectedKeys){
return true;
} else {
return false;
}
}
console.log(validateKeys(objectA,expectedKeys));
console.log(validateKeys(objectB,expectedKeys));
//first key i find not in expectedKeys return false
//if all keys in expectedKeys return true
//if there are not keys in expectedKeys are not in object
5.) To Dos--Factory Function
//function makeToDos(owner, toDos) {
//}
var makeToDos = function (owner, toDos) {
return {
owner: owner,
toDos: toDos,
makeToDos.generateHTML = function () {
let result = '';
result += '<li> eat </li> \n';
result += '<li> sleep </li> \n';
result += '<li> rave </li> \n';
result += '<li> repeat </li> \n';
};
};
}
var lisaToDos = makeToDos('Lisa', ['eat','sleep','rave','repeat']);
//function makeToDos(owner, toDos) {
// your code here
//}
//makeToDos : function (name, items, time)
//var toDosList = new toDosItems;
//toDosItems.name = name;
//toDosItems.items = items;
//toDosItems.time = time;
//toDosItems.generateHTML = function () {
//output = '';
//output += 'Name:' +
//}
//var JessiesToDos = function makeToDos;
//owner.name = "jessie";
//owner.toDos = ['eat','sleep','rave','repeat'];
//\n = new line
//makeToDos.generateHTML = function () {
//let results = '';
//results += '<li> eat </li>'
//results += '<li> sleep </li>'
//}
toast & tech 3/14/17 (messing around w/ #5 problem)
var makeToDos = function (owner, toDos) {
return {
owner: owner,
toDos: toDos,
generateHTML : function () {
result = '';
result += '<li> eat </li> \n';
result += '<li> sleep </li> \n';
result += '<li> rave </li> \n';
result += '<li> repeat </li> \n';
}
};
}
var lisaToDos = makeToDos('Lisa', ['eat','sleep','rave','repeat']);
//currently only printing the entire problem, not the answer expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment