Skip to content

Instantly share code, notes, and snippets.

@kluu1
Last active March 20, 2020 04:07
Show Gist options
  • Select an option

  • Save kluu1/af24d19a465128555ed35395ccbd419e to your computer and use it in GitHub Desktop.

Select an option

Save kluu1/af24d19a465128555ed35395ccbd419e to your computer and use it in GitHub Desktop.
const shoppingCart = ['desktop', 'keyboard', 'speakers', 'mouse'];
const addToCart = (cart, item) => {
// checks if item does not exist in cart
if (cart.indexOf(item) === -1) {
console.log(`${item} has been added to the shopping cart`);
return cart.push(item);
}
return console.log(`${item} is already in the shopping cart`);
}
addToCart(shoppingCart, 'mouse'); // mouse is already in the shopping cart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment