Back to Freecodecamp

Challenge 62: Hex to Decimal

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/68c497f3aaefc9fd9f1b0e26.md

latest1.4 KB
Original Source

--description--

Given a string representing a hexadecimal number (base 16), return its decimal (base 10) value as an integer.

Hexadecimal is a number system that uses 16 digits:

  • 0-9 represent values 0 through 9.
  • A-F represent values 10 through 15.

Here's a partial conversion table:

HexadecimalDecimal
00
11
......
99
A10
......
F15
1016
......
9F159
A0160
......
FF255
100256
  • The string will only contain characters 0–9 and A–F.

--hints--

hexToDecimal("A") should return 10.

js
assert.equal(hexToDecimal("A"), 10);

hexToDecimal("15") should return 21.

js
assert.equal(hexToDecimal("15"), 21);

hexToDecimal("2E") should return 46.

js
assert.equal(hexToDecimal("2E"), 46);

hexToDecimal("FF") should return 255.

js
assert.equal(hexToDecimal("FF"), 255);

hexToDecimal("A3F") should return 2623.

js
assert.equal(hexToDecimal("A3F"), 2623);

--seed--

--seed-contents--

js
function hexToDecimal(hex) {

  return hex;
}

--solutions--

js
function hexToDecimal(hex) {

  return parseInt(hex, 16);
}