JSFiddle - React, Tailwind, and code Playground

by Darby Rathbone

JavaScript

/* ------------------------------------------------------------------------------------------------
CHALLENGE 6

Write a function named createList that takes in an array of the current store intentory.

The inventory is formatted like this:
[
  { name: 'apples', available: true },
  { name: 'pears', available: true },
  { name: 'oranges', available: false },
  { name: 'bananas', available: true },
  { name: 'blueberries', available: false }
]

This function should use forEach to populate your grocery list based on the store's inventory. If the item is available, add it to your list. Return the final list.
------------------------------------------------------------------------------------------------ */

const createList = (availableItems) => {
  let arr = [];
  availableItems.forEach(function(e){if (e.available)arr.push(e)});
  return arr;
}


//test code
let a = [
  { name: 'apples', available: true },
  { name: 'pears', available: true },
  { name: 'oranges', available: false },
  { name: 'bananas', available: true },
  { name: 'blueberries', available: false }
];
console.log(createList(a));