Skip to content

Instantly share code, notes, and snippets.

@iancover
Last active May 4, 2017 14:07
Show Gist options
  • Save iancover/e9544497ccf2237c765a4c33aaf1b45f to your computer and use it in GitHub Desktop.
Save iancover/e9544497ccf2237c765a4c33aaf1b45f to your computer and use it in GitHub Desktop.

Revisions

  1. iancover revised this gist May 4, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion ObjDrill2_makeStudentReports.js
    Original file line number Diff line number Diff line change
    @@ -20,4 +20,5 @@ 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?
  2. iancover revised this gist May 2, 2017. 3 changed files with 126 additions and 33 deletions.
    33 changes: 0 additions & 33 deletions ObjDrill2_enrollSummerSchool.js
    Original file line number Diff line number Diff line change
    @@ -1,33 +0,0 @@
    // Enroll in Summer School Object Drill 2


    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) {
    var results = [];
    for (var i=0; i<students.length; i++) {
    var student = students[i];
    results.push({
    name: student.name,
    status: 'In Summer school',
    course: student.name,
    })
    return results;
    }
    }
    42 changes: 42 additions & 0 deletions ObjDrill2_toDoListFactory.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    // 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>';
    }
    }
    }
    84 changes: 84 additions & 0 deletions ObjDrill2_validateKeys.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    // 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;
    }
  3. iancover revised this gist Apr 27, 2017. 4 changed files with 37 additions and 6 deletions.
    20 changes: 20 additions & 0 deletions ObjDrill2_enrollInSummerSchool.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    // 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;
    }
    File renamed without changes.
    13 changes: 13 additions & 0 deletions ObjDrill2_findById.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    // 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];
    }
    }
    }
    10 changes: 4 additions & 6 deletions makeStudentReports.js → ObjDrill2_makeStudentReports.js
    Original file line number Diff line number Diff line change
    @@ -8,13 +8,11 @@
    function makeStudentsReport(data) {
    var results = [];
    for (var i=0; i<data.length; i++) {
    var itemObj = data[i];
    results.push(itemObj.name + ": " + itemObj.grade);
    }
    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`
  4. iancover revised this gist Apr 27, 2017. 1 changed file with 33 additions and 1 deletion.
    34 changes: 33 additions & 1 deletion EnrollSummerSchool.js
    Original file line number Diff line number Diff line change
    @@ -1 +1,33 @@
    // Enroll in Summer School Object Drill 2
    // Enroll in Summer School Object Drill 2


    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) {
    var results = [];
    for (var i=0; i<students.length; i++) {
    var student = students[i];
    results.push({
    name: student.name,
    status: 'In Summer school',
    course: student.name,
    })
    return results;
    }
    }
  5. iancover revised this gist Apr 27, 2017. 1 changed file with 5 additions and 26 deletions.
    31 changes: 5 additions & 26 deletions makeStudentReports.js
    Original file line number Diff line number Diff line change
    @@ -5,39 +5,18 @@
    // 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 data = [
    {
    name: 'John',
    grade: 'B',
    };
    {
    name: 'Mike',
    grade: 'C',
    };
    {
    name: 'Bob',
    grade: 'A',
    }
    ]
    return data.forEach(function(obj) {
    obj.name + ':' + obj.grade
    }
    }


    // Correct solution


    function makeStudentsReport(data) {
    var results = [];
    for (var i=0; i<data.length; i++) {
    var item = data[i];
    results.push(item.name + ': ' + item.grade);
    var itemObj = data[i];
    results.push(itemObj.name + ": " + itemObj.grade);
    }
    return results;
    }



    // Solution using .map method
    // below produces the same result as ^^, but with `.map`
    function makeStudentsReportAlt(data) {
    return data.map(function(d) {
  6. iancover created this gist Apr 26, 2017.
    1 change: 1 addition & 0 deletions EnrollSummerSchool.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    // Enroll in Summer School Object Drill 2
    46 changes: 46 additions & 0 deletions makeStudentReports.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    // 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 data = [
    {
    name: 'John',
    grade: 'B',
    };
    {
    name: 'Mike',
    grade: 'C',
    };
    {
    name: 'Bob',
    grade: 'A',
    }
    ]
    return data.forEach(function(obj) {
    obj.name + ':' + obj.grade
    }
    }


    // Correct solution


    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;
    }

    // below produces the same result as ^^, but with `.map`
    function makeStudentsReportAlt(data) {
    return data.map(function(d) {
    return d.name + ': ' + d.grade;
    });
    }