Process Inventory Data

You’ve joined a tech startup that specializes in inventory management software. Your team is working on a new feature that allows managers to transform their inventory data based on custom rules. For instance, they might want to adjust item quantities based on location or calculate item values based on their position in a list.

To achieve this, you’ve been asked to create a function that transforms each item in a dataset according to a rule provided by the manager. However, due to system requirements, you’re not allowed to use the built-in Array.map method—everything must be handled from scratch.

Write a function named processInventory that takes an array of items, arr, and a transformation function, fn. This transformation function should receive each item and its index, applying the manager’s rule to each item based on its value and position. The results should be stored in a new array, which processInventory then returns.

Example 1:
A manager wants to apply a 10% discount to the prices of products based on their position in the list (index).

const prices = [100, 200, 300];
const discount = (price, index) => price - index * 10;

const discountedPrices = processInventory(prices, discount);
console.log(discountedPrices); // Expected output: [100, 190, 280]

Example 2:
A manager wants to apply a rule where the quantity of each item in stock is doubled based on its position in the list.

const quantities = [5, 10, 15];
const doubleStock = (quantity, index) => quantity * (index + 1);

const updatedQuantities = processInventory(quantities, doubleStock);
console.log(updatedQuantities); // Expected output: [5, 20, 45]

Are you stuck on this problem?

Get detailed, step-by-step solution and improve your JavaScript skills.

  • Detailed Explanation: Each solution comes with a comprehensive explanation, helping you understand the logic and concepts thoroughly.
  • Learn to Write Better Code: Even if you have written the code, discover ways to improve it and adopt best practices.
  • Save Time: Don’t waste hours struggling—get the solution and learn efficiently.
  • Lifetime Access: Get access to lifetime solutions for all problems available
Get the Solution

" I struggled with this problem for hours, but the solution provided here was clear and easy to follow. It helped me understand where I went wrong and improved my coding skills significantly." - Jane D.

If you're not satisfied, I offer a 7-day money-back guarantee.