Functions and objects together? Is this the endtimes?
What we're doing today is writing functions that can manipulate the object passed in. We'll be using mutation, which is, again, bad practice. But we'll learn good practice soon enough, and it's pretty important that we understand JavaScript's abilities so that we understand which to use, when, and why. When we have a bug because we mutated an object passed in, we won't be able to find it without understanding mutation!
For now: mutate away!
- Fork.
- Clone.
jest --watch-all
getFirstName
- returns the value of the firstName property of the given person objectgetFirstName({firstName: 'Colin', lastName: 'Jaffe'}) // -> 'Colin'
getFirstName({firstName: 'Petra', lastName: 'Solano'}) // -> 'Petra'
getLastName
- returns the value of the lastName property of the given person objectgetLastName({firstName: 'Colin', lastName: 'Jaffe'}) // -> 'Jaffe'
getLastName({firstName: 'Petra', lastName: 'Solano'}) // -> 'Petra'
getFullName
- returns the value of the firstName property plus the lastName property with a space in the middle for the given person objectgetFullName({firstName: 'Colin', lastName: 'Jaffe'}) // -> 'Jaffe'
getFullName({firstName: 'Petra', lastName: 'Solano'}) // -> 'Petra'
setFirstName
- changes the value of the firstName property of the given person object to the given valuesetFirstName({firstName: 'Colin', lastName: 'Jaffe'}, 'Redacted') // -> {firstName: 'Redacted', lastName: 'Jaffe'}
setFirstName({firstName: 'Petra', lastName: 'Solano'}, 'Anthony') // -> {firstName: 'Anthony', lastName: 'Solano'}
setAge
- changes the value of the age property of the given person object to the given valuesetAge({firstName: 'Colin', lastName: 'Jaffe', age: 39}, 45) // -> {firstName: 'Colin', lastName: 'Jaffe', age: 45}
setAge({firstName: 'Petra', lastName: 'Solano', age: 29}, 35) // -> {firstName: 'Petra', lastName: 'Solano', age: 35}
giveBirthday
- increments by 1 the age property of the given person object, or gives them an age of 1 if they don't have that propertygiveBirthday({firstName: 'Colin', lastName: 'Jaffe', age: 39}) // -> {firstName: 'Colin', lastName: 'Jaffe', age: 40}
giveBirthday({firstName: 'Petra', lastName: 'Solano', age: 29}) // -> {firstName: 'Solano', lastName: 'Solano', age: 30}
giveBirthday({firstName: 'Baby', lastName: 'Jaffe'}) // -> {firstName: 'Baby', lastName: 'Jaffe', age: 1}
marry
- sets the marital status of both given people totrue
and sets each person'sspouseName
property to be the full name of the other
const person1 = {firstName: 'Colin', lastName: 'Jaffe', married: false}
const person2 = {firstName: 'Petra', lastName: 'Solano', married: false}
marry(person1, person2)
console.log(person1) // -> {firstName: 'Colin', lastName: 'Jaffe', married: true, spouseName: 'Petra Solano'}
console.log(person2) // -> {firstName: 'Petra', lastName: 'Solano', married: true, spouseName: 'Colin Jaffe'}
divorce
- changes the marital status tofalse
and removes thespouseName
property from both given people
const person1 = {firstName: 'Colin', lastName: 'Jaffe', married: true, spouseName: 'Petra Solano'}
const person2 = {firstName: 'Petra', lastName: 'Solano', married: true, spouseName: 'Colin Jaffe'}
divorce(person1, person2);
console.log(person1) // -> {firstName: 'Colin', lastName: 'Jaffe', married: false}
console.log(person2) // -> {firstName: 'Petra', lastName: 'Solano', married: false}
- For
getFullName
, don't set afullName
property on the given object. Just give the caller of the function back a new value based on the first name and last name. (You were probably going to do it that way anyway!) - For
giveBirthday
, you'll need to have some way to tell if the keyage
is already in the given person. Do a little research! - For
marry
, you can re-use yourgetFullName
function in order to get each person'sspouseName
. Just pass in the person whosefullName
you want, and you'll get back your newspouseName
property. - Same as above for
divorce
; you'll need to research a way to REMOVE the keyspouseName
from the given people. Simply setting the properties toundefined
ornull
won't be sufficient; they need to be as if they were never there. (Creepy!)