gpt4 book ai didi

javascript - 新手 JS 寻求帮助来完成练习

转载 作者:行者123 更新时间:2023-11-28 04:11:30 27 4
gpt4 key购买 nike

我正在学习 JS,我必须做以下练习,这是我写的,虽然不正确,但重要的是尝试一下。现在,如果有人可以帮助我更好地理解如何正确完成练习,以便理解和学习。有时我被锁住了,我写了伪代码,但我不知道如何将其变成代码,我也犯过语法错误,但正如我所说,我正在独自学习js,论坛社区是我唯一的支持。

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

/**
* 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 语句。

我知道我错了,我不明白如何使用作为 Weekday 对象数组的输入原型(prototype),这些对象是使用下面定义的原型(prototype)函数创建的,以及如何确定哪一天其他日子的流量更多.

/**
* 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;
}

最佳答案

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;
}

}

关于javascript - 新手 JS 寻求帮助来完成练习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46232973/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com