mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
chore: Merge pull request #768 from lvlte/issues/720
Changes for consolidation
This commit is contained in:
@@ -38,8 +38,13 @@ const convertArbitraryBase = (stringInBaseOne, baseOneCharacters, baseTwoCharact
|
||||
return stringInBaseTwo.replace(new RegExp(`^${baseTwoZero}+`), '')
|
||||
}
|
||||
|
||||
(() => {
|
||||
console.log(convertArbitraryBase('98', '0123456789', '01234567'))
|
||||
console.log(convertArbitraryBase('98', '0123456789', 'abcdefgh'))
|
||||
console.log(convertArbitraryBase('129', '0123456789', '01234567'))
|
||||
})()
|
||||
export { convertArbitraryBase }
|
||||
|
||||
// > convertArbitraryBase('98', '0123456789', '01234567')
|
||||
// '142'
|
||||
|
||||
// > convertArbitraryBase('98', '0123456789', 'abcdefgh')
|
||||
// 'bec'
|
||||
|
||||
// > convertArbitraryBase('129', '0123456789', '01234567')
|
||||
// '201'
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
const binaryToDecimal = (binaryString) => {
|
||||
export const binaryToDecimal = (binaryString) => {
|
||||
let decimalNumber = 0
|
||||
const binaryDigits = binaryString.split('').reverse() // Splits the binary number into reversed single digits
|
||||
binaryDigits.forEach((binaryDigit, index) => {
|
||||
decimalNumber += binaryDigit * (Math.pow(2, index)) // Summation of all the decimal converted digits
|
||||
})
|
||||
console.log(`Decimal of ${binaryString} is ${decimalNumber}`)
|
||||
return decimalNumber
|
||||
}
|
||||
|
||||
(() => {
|
||||
binaryToDecimal('111001')
|
||||
binaryToDecimal('101')
|
||||
})()
|
||||
// > binaryToDecimal('111001')
|
||||
// 57
|
||||
|
||||
// > binaryToDecimal('101')
|
||||
// 5
|
||||
|
||||
@@ -38,4 +38,4 @@ const DateDayDifference = (date1, date2) => {
|
||||
|
||||
// Example : DateDayDifference('17/08/2002', '10/10/2020') => 6630
|
||||
|
||||
module.exports = DateDayDifference
|
||||
export { DateDayDifference }
|
||||
|
||||
@@ -61,4 +61,4 @@ const DateToDay = (date) => {
|
||||
|
||||
// Example : DateToDay("18/12/2020") => Friday
|
||||
|
||||
module.exports = DateToDay
|
||||
export { DateToDay }
|
||||
|
||||
@@ -4,9 +4,16 @@ function decimalToBinary (num) {
|
||||
bin.unshift(num % 2)
|
||||
num >>= 1 // basically /= 2 without remainder if any
|
||||
}
|
||||
console.log('The decimal in binary is ' + bin.join(''))
|
||||
return bin.join('')
|
||||
}
|
||||
|
||||
decimalToBinary(2)
|
||||
decimalToBinary(7)
|
||||
decimalToBinary(35)
|
||||
export { decimalToBinary }
|
||||
|
||||
// > decimalToBinary(2)
|
||||
// '10'
|
||||
|
||||
// > decimalToBinary(7)
|
||||
// '111'
|
||||
|
||||
// > decimalToBinary(35)
|
||||
// '100011'
|
||||
|
||||
@@ -6,11 +6,22 @@ function decimalToOctal (num) {
|
||||
oct = oct + (r * Math.pow(10, c++))
|
||||
num = Math.floor(num / 8) // basically /= 8 without remainder if any
|
||||
}
|
||||
console.log('The decimal in octal is ' + oct)
|
||||
return oct
|
||||
}
|
||||
|
||||
decimalToOctal(2)
|
||||
decimalToOctal(8)
|
||||
decimalToOctal(65)
|
||||
decimalToOctal(216)
|
||||
decimalToOctal(512)
|
||||
export { decimalToOctal }
|
||||
|
||||
// > decimalToOctal(2)
|
||||
// 2
|
||||
|
||||
// > decimalToOctal(8)
|
||||
// 10
|
||||
|
||||
// > decimalToOctal(65)
|
||||
// 101
|
||||
|
||||
// > decimalToOctal(216)
|
||||
// 330
|
||||
|
||||
// > decimalToOctal(512)
|
||||
// 1000
|
||||
|
||||
@@ -22,6 +22,10 @@ function hexToDecimal (hexNum) {
|
||||
}, 0)
|
||||
}
|
||||
|
||||
// test cases
|
||||
console.log(hexToDecimal('5DE9A')) // 384666
|
||||
console.log(hexToDecimal('3D')) // 61
|
||||
export { hexToInt, hexToDecimal }
|
||||
|
||||
// > hexToDecimal('5DE9A'))
|
||||
// 384666
|
||||
|
||||
// > hexToDecimal('3D'))
|
||||
// 61
|
||||
|
||||
@@ -11,4 +11,7 @@ function hexStringToRGB (hexString) {
|
||||
return obj
|
||||
}
|
||||
|
||||
console.log(hexStringToRGB('ffffff'))
|
||||
export { hexStringToRGB }
|
||||
|
||||
// > hexStringToRGB('ffffff')
|
||||
// { r: 255, g: 255, b: 255 }
|
||||
|
||||
@@ -32,4 +32,4 @@ const LowerCaseConversion = (inputString) => {
|
||||
return newString.join('')
|
||||
}
|
||||
|
||||
module.exports = LowerCaseConversion
|
||||
export { LowerCaseConversion }
|
||||
|
||||
@@ -10,6 +10,10 @@ function octalToDecimal (num) {
|
||||
return dec
|
||||
}
|
||||
|
||||
// test cases
|
||||
console.log(octalToDecimal(56) === 46)
|
||||
console.log(octalToDecimal(2365) === 1269)
|
||||
export { octalToDecimal }
|
||||
|
||||
// > octalToDecimal(56)
|
||||
// 46
|
||||
|
||||
// > octalToDecimal(2365)
|
||||
// 1269
|
||||
|
||||
@@ -12,5 +12,10 @@ function RGBToHex (r, g, b) {
|
||||
return `#${toHex(r)}${toHex(g)}${toHex(b)}`
|
||||
}
|
||||
|
||||
console.log(RGBToHex(255, 255, 255) === '#ffffff')
|
||||
console.log(RGBToHex(255, 99, 71) === '#ff6347')
|
||||
export { RGBToHex }
|
||||
|
||||
// > RGBToHex(255, 255, 255)
|
||||
// '#ffffff'
|
||||
|
||||
// > RGBToHex(255, 99, 71)
|
||||
// '#ff6347'
|
||||
|
||||
@@ -32,4 +32,4 @@ const RailwayTimeConversion = (timeString) => {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RailwayTimeConversion
|
||||
export { RailwayTimeConversion }
|
||||
|
||||
@@ -8,7 +8,7 @@ const values = {
|
||||
M: 1000
|
||||
}
|
||||
|
||||
function romanToDecimal (romanNumber) {
|
||||
export function romanToDecimal (romanNumber) {
|
||||
let prev = ' '
|
||||
|
||||
let sum = 0
|
||||
@@ -32,7 +32,3 @@ function romanToDecimal (romanNumber) {
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
console.log(romanToDecimal('XXIIVV'))
|
||||
console.log(romanToDecimal('MDCCCIV'))
|
||||
console.log(romanToDecimal('XXIVI'))
|
||||
|
||||
15
Conversions/test/RomanToDecimal.test.js
Normal file
15
Conversions/test/RomanToDecimal.test.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import { romanToDecimal } from '../RomanToDecimal'
|
||||
|
||||
describe('romanToDecimal', () => {
|
||||
it('XXIIVV', () => {
|
||||
expect(romanToDecimal('XXIIVV')).toBe(28)
|
||||
})
|
||||
|
||||
it('MDCCCIV', () => {
|
||||
expect(romanToDecimal('MDCCCIV')).toBe(1804)
|
||||
})
|
||||
|
||||
it('XXIVI', () => {
|
||||
expect(romanToDecimal('XXIVI')).toBe(25)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user