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

id, title, challengeType, dashedName
id title challengeType dashedName
68b7cadffed0e75a517da673 Challenge 52: Binary to Decimal 28 challenge-52

--description--

Given a string representing a binary number, return its decimal equivalent as a number.

A binary number uses only the digits 0 and 1 to represent any number. To convert binary to decimal, multiply each digit by a power of 2 and add them together. Start by multiplying the rightmost digit by 2^0, the next digit to the left by 2^1, and so on. Once all digits have been multiplied by a power of 2, add the result together.

For example, the binary number 101 equals 5 in decimal because:

1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 4 + 0 + 1 = 5

--hints--

toDecimal("101") should return 5.

assert.equal(toDecimal("101"), 5);

toDecimal("1010") should return 10.

assert.equal(toDecimal("1010"), 10);

toDecimal("10010") should return 18.

assert.equal(toDecimal("10010"), 18);

toDecimal("1010101") should return 85.

assert.equal(toDecimal("1010101"), 85);

--seed--

--seed-contents--

function toDecimal(binary) {

  return binary;
}

--solutions--

function toDecimal(binary) {
  let decimal = 0;
  for (let i = 0; i < binary.length; i++) {
    const bit = parseInt(binary[binary.length - 1 - i], 10);
    decimal += bit * Math.pow(2, i);
  }
  return decimal;
}