This commit is contained in:
keithbored3310 2024-04-25 08:33:38 +08:00 committed by GitHub
commit 355fff5d87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 157 additions and 0 deletions

View File

@ -0,0 +1,7 @@
import manhattanDistance from '../manhattanDistance';
describe('manhattanDistance', () => {
it('should calculate the manhattan distance', () => {
expect(manhattanDistance(1,1)).toEqual(1);
})
})

View File

@ -0,0 +1,19 @@
//This function is calculate the manhattan distance
/**
* @param {string} x
* @param {string} y
* @return {string}
*/
export default function manhattanDistance(x, y){
let SumOfManhattanDistance = 0;
for (let i = 0; i < x.length; i++){
for (let j = 0; j < x[i].length ; j++){
//Compute the result of the Manhattan Distance
SumOfManhattanDistance += (Math.abs(x[i] -x[j])+Math.abs(y[i] - y[j]));
}
}
//Send the result back to the main function
return SumOfManhattanDistance;
}

View File

@ -0,0 +1,56 @@
Calculate is even number
* @description - Checking if number is even using divisibility by 2
*
* If number is divisible by 2 i.e remainder = 0, then it is even
* therefore, the function will return true
*
* If number is not divisible by 2 i.e remainder != 0, then it is not even i.e odd
* therefore, the function will return false
Calculate is even bitwise number
* @description - Checking if number is even using bitwise operator
* Bitwise AND (&) compares the bits of the 32
* bit binary representations of the number and
* returns a number after comparing each bit:
*
* 0 & 0 -> 0
* 0 & 1 -> 0
* 1 & 0 -> 0
* 1 & 1 -> 1
*
* For odd numbers, the last binary bit will be 1
* and for even numbers, the last binary bit will
* be 0.
*
* As the number is compared with one, all the
* other bits except the last will become 0. The
* last bit will be 0 for even numbers and 1 for
* odd numbers, which is checked with the use
* of the equality operator.
Calculate is odd number
* @description -> Checking if number is odd using not divisibility by 2
* If number is not divisible by 2 i.e remainder = 1, then it is odd
* therefore, the function will return true
*
* If number is divisible by 2 i.e remainder != 1, then it is even
* therefore, the function will return false
Calculate is odd bitwise
* @description -> Checking if number is even using bitwise operator
* Bitwise AND (&) compares the bits of the 32
* bit binary representations of the number and
* returns a number after comparing each bit:
*
* 0 & 0 -> 0
* 0 & 1 -> 0
* 1 & 0 -> 0
* 1 & 1 -> 1
*
* For every odd numbers, the last binary bit will be 1
* and for even numbers, the last binary bit will be 0.
*
* As the number is compared with one, all the
* other bits except the last will become 0. The
* last bit will be 0 for even numbers and 1 for
* odd numbers.

View File

@ -0,0 +1,23 @@
import { calcIsEven } from "../calcIsEven";
describe('calcIsEven', () => {
it('should calculate even number correctly', () => {
expect(calcIsEven(2)).toEqual(true);
expect(calcIsEven(4)).toEqual(true);
});
it('should throw an error in case if detect odd number', () =>{
expect(() => calcIsEven(3).toThrowError('It is odd number'))
});
});
import { calcIsEvenBitwise } from "../calcIsEven";
describe('calcIsEvenBitwise', () => {
it('should calculate even bitwise number correctly', () => {
expect(calcIsEvenBitwise(2)).toEqual(true);
expect(calcIsEvenBitwise(4)).toEqual(true);
});
it('should throw an error in case if detect odd number', () =>{
expect(() => calcIsEvenBitwise(3).toThrowError('It is odd bitwise number'))
});
});

View File

@ -0,0 +1,23 @@
import { calcIsOdd } from "../calcIsOdd";
describe('calcIsOdd', () => {
it('should calculate odd number correctly', () => {
expect(calcIsOdd(1)).toEqual(true);
expect(calcIsOdd(3)).toEqual(true);
});
it('should throw an error in case if detect even number', () =>{
expect(() => calcIsOdd(8).toThrowError('It is even number'))
});
});
import { calcIsOddBitwise } from "../calcIsOdd";
describe('calcIsOddBitwise', () => {
it('should calculate odd bitwise number correctly', () => {
expect(calcIsOddBitwise(1)).toEqual(true);
expect(calcIsOddBitwise(3)).toEqual(true);
});
it('should throw an error in case if detect even number', () =>{
expect(() => calcIsOddBitwise(6).toThrowError('It is even bitwise number'))
});
});

View File

@ -0,0 +1,14 @@
/**
* @function calcIsEven
* @param {number} number
* @return {boolean}
*/
export const calcIsEven = (number) => number % 2 === 0
/**
* @function calcIsEvenBitwise
* @param {number} number
* @returns {boolean}
*/
export const calcIsEvenBitwise = (number) => (number & 1) === 0

View File

@ -0,0 +1,15 @@
/**
* @function calcIsOdd
* @param {number} number
* @returns {boolean}
*/
const calcIsOdd = (number) => Boolean(number % 2) // 1 -> true, 0 -> false
/**
* @function calcIsOddBitwise
* @param {number} number
* @returns {boolean}
*/
const calcIsOddBitwise = (number) => Boolean(number & 1) // 1 -> true, 0 -> false
export { calcIsOdd, calcIsOddBitwise }