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

3.2 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69c5f3d787b1725d5f00c8b9 Challenge 255: Earth Day Cleanup Crew 28 challenge-255

--description--

Today is Earth Day. Given an array of items you cleaned up, return your total cleanup score based on the rules below.

Given items will be one of:

Item Base Value
"bottle" 10
"can" 6
"bag" 8
"tire" 35
"straw" 4
"cardboard" 3
"newspaper" 3
"shoe" 12
"electronics" 25
"battery" 18
"mattress" 38

A Rare item is represented as ["rare", value]. For example, ["rare", 80]. Rare items do not get a streak bonus.

  • Streak bonus: If the same item appears consecutively, it gets increasing bonus points.

    • First consecutive occurrence: base value
    • Second: base value + 1
    • Third: base value + 2
    • etc.
  • Fifth Item Multiplier: Every fifth item collected gets a multiplier.

    • Fifth item: *2
    • Tenth item: *3
    • etc.
  • Apply the multiplier after calculating any bonuses.

--hints--

getCleanupScore(["bottle", "straw", "shoe", "battery"]) should return 44.

assert.equal(getCleanupScore(["bottle", "straw", "shoe", "battery"]), 44);

getCleanupScore(["electronics", "straw", "newspaper", "bottle", "bag"]) should return 58.

assert.equal(getCleanupScore(["electronics", "straw", "newspaper", "bottle", "bag"]), 58);

getCleanupScore(["shoe", "can", "can", "can", "bottle", "bottle", "straw", "straw", "straw"]) should return 79.

assert.equal(getCleanupScore(["shoe", "can", "can", "can", "bottle", "bottle", "straw", "straw", "straw"]), 79);

getCleanupScore(["mattress", ["rare", 80], "tire", "tire", "tire", ["rare", 95]]) should return 358.

assert.equal(getCleanupScore(["mattress", ["rare", 80], "tire", "tire", "tire", ["rare", 95]]), 358);

getCleanupScore(["bottle", "can", "can", "shoe", "shoe", ["rare", 56], "bottle", "bottle", "can", "can", "electronics", "bottle", ["rare", 48], "bottle", "can", "can", "can", "can", "can", "can", "can"]) should return 383.

assert.equal(getCleanupScore(["bottle", "can", "can", "shoe", "shoe", ["rare", 56], "bottle", "bottle", "can", "can", "electronics", "bottle", ["rare", 48], "bottle", "can", "can", "can", "can", "can", "can", "can"]), 383);

--seed--

--seed-contents--

function getCleanupScore(items) {

  return items;
}

--solutions--

function getCleanupScore(items) {
  const values = {
    bottle: 10,
    can: 6,
    bag: 8,
    tire: 35,
    straw: 4,
    cardboard: 3,
    newspaper: 3,
    shoe: 12,
    electronics: 25,
    battery: 18,
    mattress: 38
  };

  let total = 0;
  let prevItem = null;
  let streak = 0;

  for (let i = 0; i < items.length; i++) {
    let item = items[i];
    let base;
    let isRare = Array.isArray(item) && item[0] === "rare";

    if (isRare) {
      base = item[1];
      prevItem = null;
      streak = 0;
    } else {
      base = values[item];
      streak = item === prevItem ? streak + 1 : 0;
      prevItem = item;
    }

    let points = base;
    if (!isRare) points += streak;

    if ((i + 1) % 5 === 0) {
      const multiplier = (i + 1) / 5 + 1;
      points *= multiplier;
    }

    total += points;
  }

  return total;
}