Skip to content

#10 Create binary_to_Decimal_Conversion Method in Maths Function # #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions maths/binary_to_Decimal_conversion
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @function binaryToDecimal
* @description Convert the binary to decimal .
* @param {number} binary - The input string
* @return {string} - decimal of binary.
* @example binaryToDecimal('1011') = 11
* @example binaryToDecimal('1110') = 14
*/

function binaryToDecimal(binary: string): number {
let decimal: number = 0;
let power: number = 0;
for (let i = binary.length - 1; i >= 0; i--) {
if (binary[i] === '1') {
decimal += Math.pow(2, power);
}
power++;
}
return decimal;
}


const binary: string = '1011';
const decimal: number = binaryToDecimal(binary);
console.log(`The decimal representation of ${binary} is ${decimal}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side effect. Use a proper Jest test instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check my code and merge it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side effect. Use a proper Jest test instead.

@appgurueu please check my code and merge it.

27 changes: 27 additions & 0 deletions maths/prime_check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change. Please get rid of this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check my code and merge it.

* @function isPrime
* @description program to check if a number is prime or not
* @see [Prime Check](https://www.programiz.com/javascript/examples/prime-number)
* @example Prime Number -> 5,7,11,13,17
* @param {num} number
*/

function isPrime(num: number): boolean {
if (num <= 1) {
return false;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}


const num: number = 17;
if (isPrime(num)) {
console.log(`${num} is prime`);
} else {
console.log(`${num} is not prime`);
}