Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: akshaycb/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: algorithm-for-conversion-of-binary-to-decimal-and-vice-versa
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 9 commits
  • 8 files changed
  • 1 contributor

Commits on Jul 8, 2019

  1. Copy the full SHA
    7c4a3a4 View commit details
  2. Update README.md

    akshaycb authored Jul 8, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    7e2c103 View commit details
  3. Update README.md

    akshaycb authored Jul 8, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    ff51dfa View commit details
  4. errors updated

    akshaycb committed Jul 8, 2019
    Copy the full SHA
    ef5951f View commit details
  5. added more test cases

    akshaycb committed Jul 8, 2019
    Copy the full SHA
    1e91011 View commit details

Commits on Jul 9, 2019

  1. test cases changed

    akshaycb committed Jul 9, 2019
    Copy the full SHA
    d02580d View commit details
  2. added more test cases

    akshaycb committed Jul 9, 2019
    Copy the full SHA
    286cc16 View commit details
  3. minor changes

    akshaycb committed Jul 9, 2019
    Copy the full SHA
    8f829f7 View commit details
  4. minor changes

    akshaycb committed Jul 9, 2019
    Copy the full SHA
    46be0ac View commit details
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
"description": "Algorithms and data-structures implemented on JavaScript",
"main": "index.js",
"scripts": {
"lint": "eslint ./src/*",
"lint": "eslint . --ext .js --ignore-path .gitignore",
"test": "jest",
"ci": "npm run lint && npm run test -- --coverage"
},
40 changes: 40 additions & 0 deletions src/algorithms/math/binary/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Conversion of Binary number to Decimal number
Given a binary number as input, we need to write a program to convert the given binary number into equivalent decimal number.

The idea is to extract the digits of given binary number starting from right most digit and keep a variable decimal_value. At the time of extracting digits from the binary number, multiply the digit with the proper base (Power of 2) and add it to the variable decimal_value. At the end, the variable decimal_value will store the required decimal number.

For Example:
If the binary number is 111.
decimal_value = 1*(2^2) + 1*(2^1) + 1*(2^0) = 7

![](images/binaryToDecimal.PNG)


## Reference
<a href="https://www.youtube.com/watch?v=meGcdIoTYgw">Youtube</a>

# Conversion of Decimal number to Binary number
Given a decimal number as input, we need to write a program to convert the given decimal number into equivalent binary number.


Store the remainder when the number is divided by 2 in an array.
Divide the number by 2
Repeat the above two steps until the number is greater than zero.
Print the array in reverse order now.

For Example:
If the binary number is 10.
Step 1: Remainder when 10 is divided by 2 is zero. Therefore, arr[0] = 0.
Step 2: Divide 10 by 2. New number is 10/2 = 5.
Step 3: Remainder when 5 is divided by 2 is 1. Therefore, arr[1] = 1.
Step 4: Divide 5 by 2. New number is 5/2 = 2.
Step 5: Remainder when 2 is divided by 2 is zero. Therefore, arr[2] = 0.
Step 6: Divide 2 by 2. New number is 2/2 = 1.
Step 7: Remainder when 1 is divided by 2 is 1. Therefore, arr[3] = 1.
Step 8: Divide 1 by 2. New number is 1/2 = 0.
Step 9: Since number becomes = 0. Print the array in reverse order. Therefore the equivalent binary number is 1010.

![](images/decimalToBinary.PNG)

## Reference
<a href="https://www.youtube.com/watch?v=mW8NIJ6Ns8k">Youtube</a>
9 changes: 9 additions & 0 deletions src/algorithms/math/binary/__test__/binaryToDecimal.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import binaryToDecimal from '../binaryToDecimal';

describe('Convert a binary number to decimal number', () => {
it('should return decimal number', () => {
expect(binaryToDecimal(101)).toBe(5);
expect(binaryToDecimal(1101010101)).toBe(853);
expect(binaryToDecimal(22323)).toBe('Enter the valid number');
});
});
14 changes: 14 additions & 0 deletions src/algorithms/math/binary/__test__/decimalToBinary.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import decimalToBinary from '../decimalToBinary';

describe('Convert a decimal number to binary number', () => {
it('should return decimal number', () => {
expect(decimalToBinary(0)).toBe(0);
expect(decimalToBinary(1)).toBe(1);
expect(decimalToBinary(5)).toBe(101);
expect(decimalToBinary(10)).toBe(1010);
expect(decimalToBinary(853)).toBe(1101010101);
expect(decimalToBinary(1010)).toBe(1111110010);
expect(decimalToBinary(12345)).toBe(11000000111001);
expect(decimalToBinary('abc1230')).toBe('Enter a valid decimal number');
});
});
28 changes: 28 additions & 0 deletions src/algorithms/math/binary/binaryToDecimal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param {number} binary number
* @return {number} decimal number
*/
// function to check if the number entered is binary
function checkIfBinary(binaryString) {
for (let i = 0; i < binaryString.length; i += 1) {
if (binaryString[i] > 1) {
return false;
}
}
return true;
}

export default function binaryToDecimal(binaryNumber) {
const binaryString = binaryNumber.toString();
let decimalNumber = 0;
// checks if the number entered is binary
const checkForBinary = checkIfBinary(binaryString);
if (binaryString.length !== 0 && checkForBinary === true) {
for (let i = 0; i < binaryString.length; i += 1) {
decimalNumber += binaryString[binaryString.length - 1 - i] * (2 ** i);
}
} else {
return 'Enter the valid number';
}
return decimalNumber;
}
28 changes: 28 additions & 0 deletions src/algorithms/math/binary/decimalToBinary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param {number} decimal number
* @return {number} binary number
*/
// to check if the entered number is a decimal number
function checkIfDecimal(decimalNumber) {
return !Number.isNaN(parseInt(decimalNumber, 10));
}

export default function decimalToBinary(decimalNumbers) {
const binaryNumber = [];
let decimalNumber = decimalNumbers;
const checkForDecimal = checkIfDecimal(decimalNumber);
if (checkForDecimal) {
while (decimalNumber > -1) {
if (decimalNumber > 1) {
binaryNumber.push(decimalNumber % 2);
decimalNumber = parseInt(decimalNumber / 2, 10);
} else {
binaryNumber.push(decimalNumber);
break;
}
}
} else {
return 'Enter a valid decimal number';
}
return parseInt(binaryNumber.reverse().join(''), 10);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.