add algorithm that calculates next power of two

This commit is contained in:
Indre Maksimovaite
2021-10-11 20:13:36 +03:00
parent 5f601fac8d
commit 372348f64c
4 changed files with 36 additions and 4 deletions

View File

@ -0,0 +1,15 @@
/**
*
* This script will find next power of two
* of given number.
*
*/
export const nextPowerOfTwo = (n) => {
let result = 1
while (n > 0) {
result = result << 1
n = n >> 1
}
return result
}

View File

@ -1,4 +1,4 @@
import {IsPowerOfTwo} from '../IsPowerOfTwo'
import { IsPowerOfTwo } from '../IsPowerOfTwo'
test('Check if 0 is a power of 2 or not:', () => {
const res = IsPowerOfTwo(0)

View File

@ -0,0 +1,18 @@
import { nextPowerOfTwo } from '../NextPowerOfTwo'
describe('NextPowerOfTwo', () => {
it.each`
input | result
${0} | ${1}
${1} | ${2}
${2} | ${4}
${3} | ${4}
${5} | ${8}
${125} | ${128}
${1024} | ${2048}
${10000} | ${16384}
`('returns $result when is given $input', ({ input, result }) => {
const res = nextPowerOfTwo(input)
expect(res).toBe(result)
})
})

View File

@ -40,8 +40,7 @@ const fahrenheitToRankine = (fahrenheit) => {
const kelvinToCelsius = (kelvin) => {
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
return Math.round((kelvin) - 273.15)
return Math.round((kelvin) - 273.15)
}
const kelvinToFahrenheit = (kelvin) => {
@ -53,7 +52,7 @@ const kelvinToFahrenheit = (kelvin) => {
const kelvinToRankine = (kelvin) => {
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
// Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
return Math.round(( (kelvin) * 9 / 5))
return Math.round(((kelvin) * 9 / 5))
}
const rankineToCelsius = (rankine) => {