【问题标题】:Neophyte JS seeking help to complete exerciseNeophyte JS 寻求帮助以完成锻炼
【发布时间】:2017-09-15 06:22:12
【问题描述】:

我正在学习JS,我必须做以下练习,我写的东西虽然不正确,但重要的是尝试它。现在,如果有人可以帮助我更好地理解如何正确完成练习,那么理解和学习。 在某些时候我被关起来了,我写了伪代码,但我不知道如何把它变成代码,我也犯了语法错误,但正如我所说的我一个人学习js,论坛社区是我唯一的支持。

第一个问题在于第一个函数。我不确定我是否正确地开发了它,无论是作为推理还是作为语法。要查看控制台出现的内容,我已经注销了该功能: 计算食物订单(); 他告诉我令牌丢失了。

/**
 * This function should calculate the total amount of pet food that should be
 * ordered for the upcoming week.
 * @param numAnimals the number of animals in the store
 * @param avgFood the average amount of food (in kilograms) eaten by the animals
 * 				each week
 * @return the total amount of pet food that should be ordered for the upcoming
 * 				 week, or -1 if the numAnimals or avgFood are less than 0 or non-numeric
 */
function calculateFoodOrder(numAnimals, avgFood) {
    var numAnimals = 10;
	var avgFood = numAnimals/7;
	var total = avgFood*7;
	
	if (Number(numAnimals || avgFood) < 0) and (isNaN(numAnimals || avgFood)){
		    console.log(-1);
	}
	
	return total;
}
calculateFoodOrder();

第二个是2号功能。 练习说这个函数决定了一周中哪一天访问宠物店的人数最多。 我写了一个包含星期几的数组,一个返回星期几的循环和一个 if 语句。

我知道我错了,我不明白如何将原型用于输入是一个工作日对象数组,这些对象是使用下面定义的原型函数创建的,以及如何确定哪一天的流量更多.

/**
 * Determines which day of the week had the most number of people visiting the
 * pet store. If more than one day of the week has the same, highest amount of
 * traffic, an array containing the days (in any order) should be returned.
 * (ex. ["Wednesday", "Thursday"]). If the input is null or an empty array, the function
 * should return null.
 * @param week an array of Weekday objects
 * @return a string containing the name of the most popular day of the week if there is only one most popular day, and an array of the strings containing the names of the most popular days if there are more than one that are most popular
 */
function mostPopularDays(week) {
    week = [Monday,Tuesday, Wednesday, Thursday, Friday,Saturday, Sunday];
	var weekdays = "";
	
	for (i=0; i<week.length; i++) {
		weekdays += week[i] + "<br>";
	}
	
	if (typeof week[i] === [] || week[i] === null) {
      return null;
	}
	
	/*if there is only one most popular day return "dayname";
      if there are more days than one that are most popular 
        return ["dayname","dayname","dayname"]*/
	
}

/**
 * A prototype to create Weekday objects
 */
function Weekday (name, traffic) {
    this.name = name;
    this.traffic = traffic;
}

在第三个函数的开发过程中,我不明白如何返回一个包含动物信息的对象数组,或者如果数组的长度不相等或为零,或者任何数组为空,则返回一个空数组。

**
 * Given three arrays of equal length containing information about a list of
 * animals - where names[i], types[i], and breeds[i] all relate to a single
 * animal - return an array of Animal objects constructed from the provided
 * info.
 * @param names the array of animal names
 * @param types the array of animal types (ex. "Dog", "Cat", "Bird")
 * @param breeds the array of animal breeds
 * @return an array of Animal objects containing the animals' information, or an
 *         empty array if the array's lengths are unequal or zero, or if any array is null.
 */
function createAnimalObjects(names, types, breeds) {
    names = ["Lola", "Joy", "Elohim"];
	types = ["Dog", "Cat", "Bird"];
	breeds = ["Labrador", "Siamese", "Falco"];
	
	return {
		Animal = [["Lola", "Joy", "Elohim"], ["Dog", "Cat", "Bird"], ["Labrador", "Siamese", "Falco"]];
	}
}

/**
  * A prototype to create Animal objects
  */
function Animal (name, type, breed) {
     this.name = name;
     this.type = type;
     this.breed = breed;
}

【问题讨论】:

  • 根据您的问题,很难确定您卡在哪里。请明确提及这一点,最重要的是 - 一次一个问题
  • Stack sn-p 仅用于运行代码。我刚刚将您的问题编辑为纯代码。
  • 请更详细地描述哪些部分无法正常工作(并说明您是如何测试它们的),以便我们更轻松地提供帮助。
  • 关于提问的方式,我担心它可能对任何未来的读者都没有帮助,而且听起来太宽泛了。

标签: javascript arrays function object


【解决方案1】:
function createAnimalObjects(names, types, breeds) {
    // IMPLEMENT THIS FUNCTION!
    this.names = names;
    this.types = types;
    this.breeds = breeds;
    var animal= [];
    if(names.length === types.length && names.length === breeds.length && names.length!=0 && types.length!=0 && breeds.length!=0 && names != null && types!= null && breeds != null) {
        for (i = 0;i < names.length;i++)
        {
            animal.push(new Animal(names[i], types[i], breeds[i]));
        }
        return animal[0].names;
    }
    else {
        return animal;
    }

}



function mostPopularDays(week) {
    // IMPLEMENT THIS FUNCTION!
    var resultArray = [];
    if (typeof week === 'undefined' || week === null || week.length === 0) {
        return null;
    }
    else {
        var max = 0;
        var resultArray = [];
        var resultString;
        var count = 0;
        for (i = 0; i < week.length-1; i++) {
            max = Math.max(max, week[i].traffic, week[i + 1].traffic);
        }
        for (i = 0; i < week.length; i++) {
            if (week[i].traffic === max) {
                count++;
                resultArray.push(week[i].name);
            }
        }
        if (count === 1) {
            return resultArray[0];
        }
        else {
            return resultArray;
        }

    }

}

function createAnimalObjects(names, types, breeds) {
    // IMPLEMENT THIS FUNCTION!
    this.names = names;
    this.types = types;
    this.breeds = breeds;
    var animal= [];
    if(names != null && breeds != null && types !=null) {
        if (names.length === types.length && names.length === breeds.length && names.length != 0 && types.length != 0 && breeds.length != 0) {
            for (i = 0; i < names.length; i++) {
                animal.push(new Animal(names[i], types[i], breeds[i]));
            }
            return animal;
        }
        else {
            return animal;
        }
    }
    else {
        return animal;
    }

}

【讨论】:

  • 非常感谢 Anand Bangad,您的回答澄清了很多事情。根据您的说法,第一个功能是否发展得很糟糕?我应该如何称呼它为正确的方式?在控制台中我看到这个错误:petstore.js: 15 Uncaught SyntaxError: Unexpected token { 我在这里找不到这个错误:15. if (Number (numAnimals || avgFood)
【解决方案2】:

要回答您的第一个问题...您不能在代码中使用“和”这个词,您必须使用 &&。此外,您将参数声明为函数,因此无论您做什么,它总是会返回相同的答案,这无助于使函数可重用。这是我得到的。

    function calculateFoodOrder(numAnimals, avgFood) {
  // IMPLEMENT THIS FUNCTION!
  var total = avgFood * 7;
  if(Number(numAnimals || avgFood < 0) && (isNaN(numAnimals || avgFood))) {
    return -1;
  } else {
    return total;
  }
}

【讨论】:

    【解决方案3】:

    function calculateFoodOrder(numAnimals, avgFood) {
        if ((Number(numAnimals) >= 0) && (Number(avgFood) >= 0)) {
            return (numAnimals * avgFood);
        } else {
            return -1;
        }
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多