Last active
April 25, 2017 17:51
-
-
Save iancover/46c7ac473155b8e0714919d25dc3a820 to your computer and use it in GitHub Desktop.
Object Drills
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
// Create My Object Drill | |
// Write a function that returns an object with key/value pairs: foo=>bar, answerToUniverse=>42, olly olly=>oxen, and | |
// sayHello=> function that returns the string 'hello' | |
function createMyObject() { | |
return myObject = { | |
foo: 'bar', | |
answerToUniverse: 42, | |
'olly olly': 'oxen free', | |
sayHello: function myHello() { | |
return 'hello'; | |
} | |
}; | |
} | |
// Just corrected the sayHello key, in this case, the assignmt | |
// asked to write a function as a value for that key. |
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
// Modify the keyDeleter function so that it deletes keys | |
// for foo and bar for any object passed in, and then | |
// returns the modified object. | |
function keyDeleter(obj) { | |
delete obj.foo; | |
delete obj.bar; | |
return obj; | |
} | |
var sampleObj = { | |
foo: 'foo', | |
bar: 'bar', | |
bizz: 'bizz', | |
bang: 'bang' | |
}; |
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
// Modify the personMaker.fullName to be a function that uses self reference | |
// (via this) in order to return the firstName and lastName properties | |
// separated by a space. | |
// So, for instance, if firstName was 'Jane' and lastName was 'Doe', | |
// fullName() should return 'Jane Doe'. | |
function personMaker() { | |
var person = { | |
firstName: 'Paul', | |
lastName: 'Jones', | |
fullName: null | |
}; | |
this.fullName = function myFullName(firstName,lastName) { | |
return firstName + "" + lastName; | |
}; | |
return person; | |
} | |
// Was reading problem wrong, states: 'Modify personMaker.fullname...' | |
// Correct Solution: | |
function personMaker() { | |
var person = { | |
firstName: 'Paul', | |
lastName: 'Jones', | |
fullName: function() { | |
return this.firstName + ' ' + this.lastName | |
} | |
}; | |
return person; | |
} |
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
//Modify the updateObject (which takes a single argument (obj)) | |
//so that it adds the following key/value pairs to obj and | |
//returns it: | |
// foo => 'foo' | |
// bar => 'bar' | |
// bizz => 'bizz' | |
// bang => 'bang' | |
function updateObject(obj) { | |
obj.foo = 'foo'; | |
obj.bar = 'bar'; | |
obj.bizz = 'bizz'; | |
obj.bang = 'bang'; | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment