【发布时间】: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