/* Task 3 *///狗喂食器--根据它们的体重和年龄,我们需要知道每天要喂狗多少磅的食物!/*使用下面的hungryDog函数和喂养要求来完成以下操作:
记住:这个程序应该正确处理成人和小狗的年龄和体重。
喂食要求:
成年狗只1岁及以上,可达5磅5%体重6- 10磅4%体重11 - 15磅体重3%体重>15磅- 2%体重
小于1岁的幼犬2-4个月10%的体重4-7个月5%的体重7-12个月体重的4%
注:如果正确操作,15磅的体重和1岁的年龄将返回0.44999999999999996 */
function hungryDog(age,weight){
if (age >= 1){
if (weight <= 5){
let foodAmount = weight * .05;
}
else if (weight <= 10){
let foodAmount = weight * .04;
}
else if (weight <= 15) {
let foodAmount = weight * .03;
}
else if (weight > 15) {
let foodAmount = weight * .02;
}
}
else if (age < 1) {
if (age <= .33) {
let foodAmount = weight * .10;
}
else if (age <=.5833) {
let foodAmount = weight * .05;
}
else if (age < 1) {
let foodAmount = weight * .04;
}
}
return foodAmount;
}
console.log(hungryDog(1,15))我不知道我在这里错过了什么。请帮帮我!没有定义错误读取foodAmount。
发布于 2021-04-13 21:38:25
您需要在顶部声明您的foodAmount,这样您就可以在一些if-else块中更新它的值,然后在最后返回。按照您声明它的方式,foodAmount只在if- end块中可用,但是当您试图在函数结束时返回它时,它没有定义。
function hungryDog(age,weight){
let foodAmount;
if (age >= 1){
if (weight <= 5){
foodAmount = weight * .05;
}
else if (weight <= 10){
foodAmount = weight * .04;
}
else if (weight <= 15) {
foodAmount = weight * .03;
}
else if (weight > 15) {
foodAmount = weight * .02;
}
}
else if (age < 1) {
if (age <= .33) {
foodAmount = weight * .10;
}
else if (age <=.5833) {
foodAmount = weight * .05;
}
else if (age < 1) {
foodAmount = weight * .04;
}
}
return foodAmount;
}
https://stackoverflow.com/questions/67082677
复制相似问题