雄辩JS ch6宠物猫练习



这是我第一次使用堆栈溢出。我只是想用下面大学作业中的练习做一个简单的宠物猫。代码工作得很好,但不确定我的代码是否回答了减少的部分对象属性。如果我能得到一些反馈,让它变得更好,我会非常感激。我是编码新手。谢谢你。

//问题给自己写一只虚拟的猫——用CLI的动物比有毛的动物好多了。

创建一个对象表示一只猫。它应该有抗疲劳、抗饥饿、抗孤独、抗快乐的特性接下来,编写增加和减少这些属性的方法。给它们取一些能代表增加或减少这些东西的名字,比如"喂食"、"睡眠"或"宠物"。最后,编写一个方法,打印出猫在每个区域的状态。(要有创意,例如:Paws真的很饿,Paws很高兴。)

//我的代码
let cat=  {
name: "Roy",
tiredness:0,
hunger:0,
loneliness:0,
happiness:0,
//increase
feed: function(fullness){
let x= fullness + this.hunger
console.log(`${this.name} is ${x} %  full `)
},
energized:function (hour){
let x= hour+this.tiredness
console.log(`${this.name} is ${x} %  energized `)
},
socialLife:function(hangout){
let x= hangout+this.happiness
console.log(`${this.name} is ${x} %  happy `)
},
//decrease
feedMinus:function(gettingHungry){
let x= gettingHungry-this.hunger
console.log(`${this.name} is getting ${x} %  hungry `)
},
sleep:function (gettingSleepy){
let x= gettingSleepy-this.tiredness
console.log(`${this.name} is getting ${x} %  sleepy `)
},
isolated:function(alone){
let x= alone-this.happiness
console.log(`${this.name} is getting ${x} %  isolated `)
}
}

你的方法永远不会改变你的属性值。

试试这个:

feed: function(fullness) {
this.hunger += fullness;
console.log(`${this.name} is ${this.hunger} %  full`);
}

也许你应该给你的值加上一些限制,比如饥饿不能小于0%,也不能大于100%。使用Math.max()和Math.min () .

最新更新