trouble multiplying in JavaScript

     1. 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 }
];

// write the shouldWeOrderThisCandy function
function shouldWeOrderThisCandy(inventory){
  for (i = 0; i < inventory.length; i++)


if (inventory[i].inStock < inventory[i].weeklyAverage){ 
for (j = 0; j < inventory[i].weelyAverage.length; j++){
      return inventory[i].weeklyaverage[j] * 2;
   } else {
      return 0;
    }
  }
}

So what I am trying to do here in JavaScript is find out is i want to order more candy or not. So far I compare the in Stock to the weekly average and if in Stock is < weekly average I will order 2 times the weekly Average. However, if the is greater than in Stock then i will order nothing. Here's my code so far. My function should take on 2 arguments which is inventory and candy here. I cant figure out why it returning nothing but zero, when it should be letting me know how much candy to order. please help example when called with "Twizzlers" it should return 400 since weekly Average for "Skittles" is 200 and 200 * 2 is 400 and so forth.

2 answers

  • answered 2020-08-10 23:15 iota

    1. You need to accept the item name as the second parameter.
    2. You can use Array#find to find the inventory item with the specified name.

    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 }
    ];
    function shouldWeOrderThisCandy(inventory, candy){
      const obj = inventory.find(x=>x.candy===candy);
      if(obj?.inStock < obj?.weeklyAverage) return obj.weeklyAverage * 2;
      else return 0;
    }
    console.log(shouldWeOrderThisCandy(inventory, "Twizzlers"));

  • answered 2020-08-10 23:29 Jesse Nguyen

    There are a couple of issues that immediately jump out at me.

    The first one is that you say that you're calling the function with two parameters, but the way you've written your function it only accepts one.

    The second issue I see is in your second for loop. You've misspelled "weeklyAverage".

    The third issue I see is I don't see why you even have a second for loop. It looks like you're trying to iterate over each digit of the weeklyAverage value and then multiply it by 2, but you could just simply multiply weeklyAverage by 2 instead of attempting to do it digit by digit. This also leads into the fourth issue:

    You're returning a value inside of your second for loop. When your code reaches that point, it will immediately return whatever value is there and then stop running, so although you have a for loop that you're expecting to run multiple times, it will only run the first iteration and then return.

    So in order to make this work, you'll need to do a few things:

    1. Alter your function declaration so that it accepts two arguments instead of one. The first argument can be the actual inventory object (like the way you have it set up now) and then the second argument can be a string containing the name of the candy you want to look at.
    2. After you've done that, you should no longer need any for loops in your function, because you'll no longer need to manually iterate over every single candy in your inventory object. Instead, you'll need to find a method that you can use to search for the candy argument inside of the inventory object.
    3. Once you've found the candy inside your inventory object, simply compare the inStock and weeklyAverage properties. If inStock is too low, return weeklyAverage multiplied by two, otherwise return zero.