Good afternoon! Here's our prompt for today.
Today, we're going to do some math! There is only one prerequisite for answering this question, and it is knowing the definitions of binary
and decimal
.
According to How to Convert Decimal to Binary and Binary to Decimal, binary
is a numeric system that only uses two digits — 0 and 1. Computers operate in binary, meaning they store data and perform calculations using only zeros and ones.
Decimal
, on the other hand, is "a number system that uses a notation in which each number is expressed in base 10 by using one of the first nine integers or 0 in each place and letting each place value be a power of 10."

Can you write a function that returns the binary string of a given decimal number? For example, if the input was 3
:
1decimalToBinary(3);
2// 11
We get 11
because it is the binary representation of 3
(1 x 2 + 1 x 1).
Constraints
- The given number will be a positive integer in the range between 0 and
1000000000
- Expected time complexity :
O(log n)
- Expected space complexity :
O(1)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
var assert = require('assert');
function decimalToBinary(num) {
return num;
}
try {
assert.equal(decimalToBinary(3), 11);
console.log('PASSED: ' + 'decimalToBinary(3) should return 11');
} catch (err) {
console.log(err);
}
try {
assert.equal(decimalToBinary(8), 1000);
console.log('PASSED: ' + 'decimalToBinary(8) should return 1000');
} catch (err) {
console.log(err);
}
try {
assert.equal(decimalToBinary(1000), 1111101000);
console.log('PASSED: ' + 'decimalToBinary(1000) should return 1111101000');
} catch (err) {
console.log(err);
}
We'll now take you through what you need to know.
How do I use this guide?