新手JS寻求帮助以完成锻炼

时间:2017-09-15 06:22:12

标签: javascript arrays function object

我学习JS并且我必须做以下练习,我写的东西,虽然不正确,重要的是尝试它。现在,如果有人能帮助我更好地了解如何正确完成运动,那么就要理解和学习。 在我被锁定的某些点上,我编写了伪代码,但我不知道如何将其转换为代码,我也犯了语法错误,但正如我所说,我只是学习js和论坛社区是我唯一的支持。

第一个问题出在第一个功能中。我不确定我是否正确地开发它,无论是作为推理还是作为语法。看到控制台出来的东西我已经注销了这个功能: calculateFoodOrder(); 他告诉我,代币丢失了。



/**
 * 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();
&#13;
&#13;
&#13;

第二个是数字2功能。 练习说这个功能决定了一周中哪一天访问宠物商店的人数最多。 我写了一个数组,其中包含一周中的日期,一个周期来返回一周的日期和一个if语句。

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

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

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

&#13;
&#13;
**
 * 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;
}
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

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

}

答案 1 :(得分:0)

要回答你的第一个问题......你不能使用&#34;和&#34;在你的代码中,你必须使用&amp;&amp ;.此外,您将参数声明为函数,因此无论您执行什么操作,它都将返回相同的答案,这并不能帮助使函数可重用。这就是我得到的。

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

答案 2 :(得分:0)

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

相关问题