Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.7 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6821ebe4237de8297eaee794 Challenge 18: Second Best 28 challenge-18

--description--

Given an array of integers representing the price of different laptops, and an integer representing your budget, return:

  1. The second most expensive laptop if it is within your budget, or
  2. The most expensive laptop that is within your budget, or
  3. 0 if no laptops are within your budget.
  • Duplicate prices should be ignored.

--hints--

getLaptopCost([1500, 2000, 1800, 1400], 1900) should return 1800

assert.equal(getLaptopCost([1500, 2000, 1800, 1400], 1900), 1800);

getLaptopCost([1500, 2000, 2000, 1800, 1400], 1900) should return 1800

assert.equal(getLaptopCost([1500, 2000, 2000, 1800, 1400], 1900), 1800);

getLaptopCost([2099, 1599, 1899, 1499], 2200) should return 1899

assert.equal(getLaptopCost([2099, 1599, 1899, 1499], 2200), 1899);

getLaptopCost([2099, 1599, 1899, 1499], 1000) should return 0

assert.equal(getLaptopCost([2099, 1599, 1899, 1499], 1000), 0);

getLaptopCost([1200, 1500, 1600, 1800, 1400, 2000], 1450) should return 1400

assert.equal(getLaptopCost([1200, 1500, 1600, 1800, 1400, 2000], 1450), 1400);

--seed--

--seed-contents--

function getLaptopCost(laptops, budget) {

  return laptops;
}

--solutions--

function getLaptopCost(laptops, budget) {
  laptops = [...new Set(laptops)].sort((a, b) => b - a);

  if (budget >= laptops[1]) return laptops[1];
  if (budget < laptops[laptops.length - 1]) return 0;

  for (let i = 2; i < laptops.length; i++) {
    if (budget >= laptops[i]) return laptops[i];
  }
}