Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ManishPoduval/5eb4982718dcf50d7eca2bd3d11cd4cd to your computer and use it in GitHub Desktop.
Save ManishPoduval/5eb4982718dcf50d7eca2bd3d11cd4cd to your computer and use it in GitHub Desktop.
// 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