Forked from sandrabosk/solution-objects-this-chuck.js
Last active
January 17, 2021 05:53
-
-
Save ManishPoduval/5eb4982718dcf50d7eca2bd3d11cd4cd to your computer and use it in GitHub Desktop.
This file contains 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
// TODO: write the methods getAge, addFriend and getRandomFriend | |
const chuck = { | |
firstName: 'Chuck', | |
lastName: 'Norris', | |
birthDate: new Date('1940-03-10'), | |
friends: [ | |
'Alvaro', | |
'Luis' | |
], | |
displayInfo() { | |
console.log( | |
`My name is ${this.firstName} ${this.lastName} and I have ${this.friends.length} friends.` | |
); | |
}, | |
getAge() { | |
let age = Math.floor((new Date() - this.birthDate) / 1000 / 60 / 60 / 24 / 365.25); | |
return age; | |
}, | |
addFriend(name) { | |
this.friends.push(name); | |
}, | |
getRandomFriend() { | |
let randomIndex = Math.floor(Math.random() * this.friends.length); | |
return this.jokes[randomIndex]; | |
} | |
}; | |
chuck.displayInfo(); | |
console.log('getAge', chuck.getAge()); // Should return 80 if you are in 2020 | |
chuck.addFriend('Jorge'); | |
console.log('getRandomFriend', chuck.getRandomFriend()); | |
chuck.addFriend('Helena'); | |
console.log('getRandomFriend', chuck.getRandomFriend()); | |
chuck.addFriend('Manish'); | |
console.log('getRandomFriend', chuck.getRandomFriend()); | |
chuck.displayInfo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment