Skip to content

Instantly share code, notes, and snippets.

@iancover
Last active April 25, 2017 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iancover/46c7ac473155b8e0714919d25dc3a820 to your computer and use it in GitHub Desktop.
Save iancover/46c7ac473155b8e0714919d25dc3a820 to your computer and use it in GitHub Desktop.
Object Drills
// 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.
// 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'
};
// 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;
}
//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