Neophyte JS seeking help to complete exercise

  You are currently not logged in. You can view the forums, but cannot post messages. Log In | Register

15-Sep-17 01:39
I'm learning JS and I have to do the following exercise, something I wrote, though incorrect, the important thing is to try it. Now, if someone can kindly help me to understand better how to properly complete exercise, so to understand and learn. At some points I'm locked up, I wrote the pseudocode, but I do not know how to turn it into code, I've also made syntax mistakes, but as I said I'm learning js alone and forum communities are my only support.

The first problem is in the first function. I'm not sure I developed it properly, both as a reasoning and as a syntax. to see console what was coming out I had written off the function:
calculateFoodOrder ();
It tells me that tokens are missing.


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


The second is number 2 function.
Exercise says this function determines which day of the week had the most number of people visiting the pet store.
I wrote an array with the days of the week, a cycle for returning the days of the week and an if statment.

I know I'm wrong, I did not understand how to use the prototype for input that is an array of Weekday objects, which are created using the prototype function defined below, and how to determine which day has more traffic other days.


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


In the development of the third function I did not understand how to return an array of objects that contain the animals' information, or an empty array if the array's lengths are unequal or zero, or if any array is null.


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

Thanks!!

[Edited by aury on 15-Sep-17 08:05]

--
Aurora Ruggieri
15-Sep-17 17:54
Your function definition requires two parameters

function calculateFoodOrder(numAnimals, avgFood)



Your function call passes none.

calculateFoodOrder();


*******************************************************************


"I did not understand how to return an array of objects that contain the animals' information,"



return ArrayName;


[Edited by chrishirst on 15-Sep-17 17:54]

--
Chris.
So long, and thanks for all the fish.
http://webmaster-talk.eu/

 
New posts
Old posts

Follow Elated