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