mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
Conversions algorithms : remove live code & console.log.
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'
|
||||
|
@ -8,7 +8,10 @@ const binaryToDecimal = (binaryString) => {
|
||||
return decimalNumber
|
||||
}
|
||||
|
||||
(() => {
|
||||
binaryToDecimal('111001')
|
||||
binaryToDecimal('101')
|
||||
})()
|
||||
export { binaryToDecimal }
|
||||
|
||||
// > binaryToDecimal('111001')
|
||||
// 57
|
||||
|
||||
// > binaryToDecimal('101')
|
||||
// 5
|
||||
|
@ -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,11 @@ 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,8 @@ function hexStringToRGB (hexString) {
|
||||
return obj
|
||||
}
|
||||
|
||||
console.log(hexStringToRGB('ffffff'))
|
||||
export { hexStringToRGB }
|
||||
|
||||
|
||||
// > hexStringToRGB('ffffff')
|
||||
// { r: 255, g: 255, b: 255 }
|
||||
|
@ -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'
|
||||
|
@ -33,6 +33,4 @@ function romanToDecimal (romanNumber) {
|
||||
return sum
|
||||
}
|
||||
|
||||
console.log(romanToDecimal('XXIIVV'))
|
||||
console.log(romanToDecimal('MDCCCIV'))
|
||||
console.log(romanToDecimal('XXIVI'))
|
||||
export { romanToDecimal }
|
||||
|
Reference in New Issue
Block a user