Create a function called shouldWeOrderThisCandy that takes in:
the inventory array a specific type of candy (string) The function should find the candy in the array that matches the name passed in.
When that candy is found, return true if the number inStock is less than the weeklyAverage. Otherwise, return false.
If the array doesn't have any candy with that name, return false.
Example input and output:
shouldWeOrderThisCandy(inventory, "Twizzlers"); //-> true
shouldWeOrderThisCandy(inventory, "Sour Patch Kids"); //-> true
shouldWeOrderThisCandy(inventory, "Milk Duds"); //-> false
shouldWeOrderThisCandy(inventory, "Now and Laters"); //-> false
shouldWeOrderThisCandy(inventory, "Broccoli Bits"); //-> false
Sample inventory array This is a sample of the data that will be used in all of the challenges:
let inventory = [
{ candy: "Twizzlers", inStock: 180, weeklyAverage: 200 },
{ candy: "Sour Patch Kids", inStock: 90, weeklyAverage: 100 },
{ candy: "Milk Duds", inStock: 300, weeklyAverage: 170 },
{ candy: "Now and Laters", inStock: 150, weeklyAverage: 40 }
];
The tests might pass in different inventory arrays, but they'll have the same shape.
This is my code. I've tried for loops and without. if statements and ternary expressions. When I used a for loop, it revered my passes and fails.
function shouldWeOrderThisCandy(inventory, candy){
const e = inventory;
const whichCandy = e.candy;
const match = candy === whichCandy;
const final = match && e.inStock < e.weeklyAverage ? true : false;
return final;
}
Test Results: Passed:4 Failed:3 Exit Code:3 failed: returns true for Twizzlers, since inStock is less than weeklyAverage failed: returns true for Sour Patch Kids, since inStock is less than weeklyAverage passed: returns false for Milk Duds, since inStock is more than weeklyAverage passed: returns false for Now and Laters, since inStock is more than weeklyAverage passed: when inventory is an empty array, returns false for Twizzlers, since there's no candy in the array failed: returns false for Broccoli Bites, since no candy with that name is in the inventory failed: returns true for Kit Kat when a different inventory is passed in, since inStock is less than weeklyAverage
CodePudding user response:
The trick I guess here is to find
the correct candy by the property candy
. Check out Array.find
method below (or "manually do a for loop over the array to find the item).
let inventory = [
{ candy: "Twizzlers", inStock: 180, weeklyAverage: 200 },
{ candy: "Sour Patch Kids", inStock: 90, weeklyAverage: 100 },
{ candy: "Milk Duds", inStock: 300, weeklyAverage: 170 },
{ candy: "Now and Laters", inStock: 150, weeklyAverage: 40 }
];
console.log(shouldWeOrderThisCandy(inventory, "Twizzlers")); //-> true
console.log(shouldWeOrderThisCandy(inventory, "Sour Patch Kids")); //-> true
console.log(shouldWeOrderThisCandy(inventory, "Milk Duds")); //-> false
console.log(shouldWeOrderThisCandy(inventory, "Now and Laters")); //-> false
console.log(shouldWeOrderThisCandy(inventory, "Broccoli Bits")); //-> false
function shouldWeOrderThisCandy(inventory, candy) {
const whichCandy = inventory.find(function(item) {
return item.candy === candy
})
if (!whichCandy) {
return false;
}
const final = whichCandy.inStock < whichCandy.weeklyAverage ? true : false;
return final;
}
CodePudding user response:
Let me help you by explaining what your code actually does:
function shouldWeOrderThisCandy(inventory, candy){
// assign inventory to e
// not really necessary but if you prefer using e instead of inventory thats fine
const e = inventory;
// here you look for an entry names "candy" in the inventory
const whichCandy = e.candy;
// here you check if your passed in candy variable matches undefined from previous result
// which always returns false
const match = candy === whichCandy;
// here your code will always return false since match is false
// it never actually checks the stock
const final = match && e.inStock < e.weeklyAverage ? true : false;
return final;
}
I guess what you want to do is search the array for an entry that has a candy
value that matches your candy variable
here are some hints:
const match = e.find((item) => item.candy === candy);
const final = match && match.inStock < match.weeklyAverage ? true : false;