Skip to content

Instantly share code, notes, and snippets.

@Chojiu15
Created April 8, 2021 16:17
Show Gist options
  • Save Chojiu15/9f4d04c26d9d8ded7410845c955cbae9 to your computer and use it in GitHub Desktop.
Save Chojiu15/9f4d04c26d9d8ded7410845c955cbae9 to your computer and use it in GitHub Desktop.
Cat OOP
// // Create an object that represents a cat. It should have properties for tiredness, hunger, loneliness and happiness
// // Next, write methods that increase and decrease those properties. Call them something that actually represents what would increase or decrease these things,
// like "feed", "sleep", or "pet".
// // Last, write a method that prints out the cat's status in each area. (Be creative e.g. Paws is really hungry, Paws is VERY happy.)
// // Bonus: Make the functions take arguments that increase or decrease arbitrary amounts
// // Bonus: Make the functions as arbitrary (or crazy) as cats are - sometimes make it so the cat doesn't want to be
// // petted :slight_smile:
const randomNumber = (number) => Math.floor(Math.random() * number)
class Cat {
constructor(name){
this._name = name
this._tiredness = randomNumber(100)
this._hunger = randomNumber(100)
this._loneliness = randomNumber(100)
this._hapiness = randomNumber(100)
}
feed(food){
let feedMood
if(this._hunger < 33){
feedMood = `${this._name} is really hungry`
this._hunger += food
}
if(this._hunger > 33 || this._hunger < 66){
feedMood = `${this._name} had enough food`
this._hunger -= food
}
return feedMood
}
information(){
return{
name : this._name,
tiredness : this._tiredness,
hunger : this._hunger,
loneliness : this._loneliness,
happiness : this._hapiness
}
}
}
const Paws = new Cat('Paws')
console.log(Paws.information())
console.log(Paws.feed(10))
console.log(Paws.information())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment