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.9 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
68b1f72371a5ac895ac70a04 Challenge 41: File Storage 28 challenge-41

--description--

Given a file size, a unit for the file size, and hard drive capacity in gigabytes (GB), return the number of files the hard drive can store using the following constraints:

  • The unit for the file size can be bytes ("B"), kilobytes ("KB"), or megabytes ("MB").
  • Return the number of whole files the drive can fit.
  • Use the following conversions:
Unit Equivalent
1 B 1 B
1 KB 1000 B
1 MB 1000 KB
1 GB 1000 MB

For example, given 500, "KB", and 1 as arguments, determine how many 500 KB files can fit on a 1 GB hard drive.

--hints--

numberOfFiles(500, "KB", 1) should return 2000.

assert.equal(numberOfFiles(500, "KB", 1), 2000);

numberOfFiles(50000, "B", 1) should return 20000.

assert.equal(numberOfFiles(50000, "B", 1), 20000);

numberOfFiles(5, "MB", 1) should return 200.

assert.equal(numberOfFiles(5, "MB", 1), 200);

numberOfFiles(4096, "B", 1.5) should return 366210.

assert.equal(numberOfFiles(4096, "B", 1.5), 366210);

numberOfFiles(220.5, "KB", 100) should return 453514.

assert.equal(numberOfFiles(220.5, "KB", 100), 453514);

numberOfFiles(4.5, "MB", 750) should return 166666.

assert.equal(numberOfFiles(4.5, "MB", 750), 166666);

--seed--

--seed-contents--

function numberOfFiles(fileSize, fileUnit, driveSizeGb) {

  return fileSize;
}

--solutions--

function numberOfFiles(fileSize, fileUnit, driveSizeGb) {
  const driveSizeBytes = driveSizeGb * 1000 * 1000 * 1000;

  let fileSizeBytes;
  if (fileUnit === "B") {
    fileSizeBytes = fileSize;
  } else if (fileUnit === "KB") {
    fileSizeBytes = fileSize * 1000;
  } else {
    fileSizeBytes = fileSize * 1000 * 1000;
  }

  return Math.floor(driveSizeBytes / fileSizeBytes);
}