Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Created April 22, 2020 19:28
Show Gist options
  • Save sandrabosk/4d8b5a8c1d002e007c1b0944a2b40730 to your computer and use it in GitHub Desktop.
Save sandrabosk/4d8b5a8c1d002e007c1b0944a2b40730 to your computer and use it in GitHub Desktop.
// TODO: write the methods getAge, addJoke and getRandomJoke
const chuck = {
firstName: 'Chuck',
lastName: 'Norris',
birthDate: new Date('1940-03-10'),
jokes: [
'Chuck Norris counted to infinity... Twice.',
'Chuck Norris is the only man to ever defeat a brick wall in a game of tennis'
],
displayInfo() {
console.log(
`My name is ${this.firstName} ${this.lastName} and I have ${this.jokes.length} jokes.`
);
},
getAge() {
return Math.floor(
(new Date() - this.birthDate) / 1000 / 60 / 60 / 24 / 365.25
);
},
addJoke(joke) {
this.jokes.push(joke);
},
getRandomJoke() {
let randomIndex = Math.floor(Math.random() * this.jokes.length);
return this.jokes[randomIndex];
}
};
chuck.displayInfo();
console.log('getAge', chuck.getAge());
chuck.addJoke('Chuck Norris can divide by zero.');
console.log('getRandomJoke', chuck.getRandomJoke());
chuck.addJoke('Chuck Norris kills flies with his gun.');
console.log('getRandomJoke', chuck.getRandomJoke());
chuck.addJoke('Chuck Norris was once in a knife fight, and the knife lost.');
console.log('getRandomJoke', chuck.getRandomJoke());
chuck.displayInfo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment