Back to Freecodecamp

Challenge 161: Free Shipping

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/694596b0585c11170ac7c7f9.md

latest1.8 KB
Original Source

--description--

Given an array of strings representing items in your shopping cart, and a number for the minimum order amount to qualify for free shipping, determine if the items in your shopping cart qualify for free shipping.

The given array will contain items from the list below:

ItemPrice
"shirt"34.25
"jeans"48.50
"shoes"75.00
"hat"19.95
"socks"15.00
"jacket"109.95

--hints--

getsFreeShipping(["shoes"], 50) should return true.

js
assert.isTrue(getsFreeShipping(["shoes"], 50));

getsFreeShipping(["hat", "socks"], 50) should return false.

js
assert.isFalse(getsFreeShipping(["hat", "socks"], 50));

getsFreeShipping(["jeans", "shirt", "jacket"], 75) should return true.

js
assert.isTrue(getsFreeShipping(["jeans", "shirt", "jacket"], 75));

getsFreeShipping(["socks", "socks", "hat"], 75) should return false.

js
assert.isFalse(getsFreeShipping(["socks", "socks", "hat"], 75));

getsFreeShipping(["shirt", "shirt", "jeans", "socks"], 100) should return true.

js
assert.isTrue(getsFreeShipping(["shirt", "shirt", "jeans", "socks"], 100));

getsFreeShipping(["hat", "socks", "hat", "jeans", "shoes", "hat"], 200) should return false.

js
assert.isFalse(getsFreeShipping(["hat", "socks", "hat", "jeans", "shoes", "hat"], 200));

--seed--

--seed-contents--

js
function getsFreeShipping(cart, minimum) {

  return cart;
}

--solutions--

js
function getsFreeShipping(cart, minimum) {
  const prices = {
    shirt: 34.25,
    jeans: 48.50,
    shoes: 75.00,
    hat: 19.95,
    socks: 15.00,
    jacket: 109.95
  };

  let total = 0;

  for (const item of cart) {
    total += prices[item];
  }

  return total >= minimum;
}