Conversions algorithms : remove live code & console.log.

This commit is contained in:
Eric Lavault
2021-10-09 18:15:30 +02:00
parent ab7e51909a
commit 30779682b9
9 changed files with 73 additions and 31 deletions

View File

@ -38,8 +38,13 @@ const convertArbitraryBase = (stringInBaseOne, baseOneCharacters, baseTwoCharact
return stringInBaseTwo.replace(new RegExp(`^${baseTwoZero}+`), '') return stringInBaseTwo.replace(new RegExp(`^${baseTwoZero}+`), '')
} }
(() => { export { convertArbitraryBase }
console.log(convertArbitraryBase('98', '0123456789', '01234567'))
console.log(convertArbitraryBase('98', '0123456789', 'abcdefgh')) // > convertArbitraryBase('98', '0123456789', '01234567')
console.log(convertArbitraryBase('129', '0123456789', '01234567')) // '142'
})()
// > convertArbitraryBase('98', '0123456789', 'abcdefgh')
// 'bec'
// > convertArbitraryBase('129', '0123456789', '01234567')
// '201'

View File

@ -8,7 +8,10 @@ const binaryToDecimal = (binaryString) => {
return decimalNumber return decimalNumber
} }
(() => { export { binaryToDecimal }
binaryToDecimal('111001')
binaryToDecimal('101') // > binaryToDecimal('111001')
})() // 57
// > binaryToDecimal('101')
// 5

View File

@ -4,9 +4,16 @@ function decimalToBinary (num) {
bin.unshift(num % 2) bin.unshift(num % 2)
num >>= 1 // basically /= 2 without remainder if any num >>= 1 // basically /= 2 without remainder if any
} }
console.log('The decimal in binary is ' + bin.join('')) return bin.join('')
} }
decimalToBinary(2) export { decimalToBinary }
decimalToBinary(7)
decimalToBinary(35) // > decimalToBinary(2)
// '10'
// > decimalToBinary(7)
// '111'
// > decimalToBinary(35)
// '100011'

View File

@ -6,11 +6,22 @@ function decimalToOctal (num) {
oct = oct + (r * Math.pow(10, c++)) oct = oct + (r * Math.pow(10, c++))
num = Math.floor(num / 8) // basically /= 8 without remainder if any num = Math.floor(num / 8) // basically /= 8 without remainder if any
} }
console.log('The decimal in octal is ' + oct) return oct
} }
decimalToOctal(2) export { decimalToOctal }
decimalToOctal(8)
decimalToOctal(65) // > decimalToOctal(2)
decimalToOctal(216) // 2
decimalToOctal(512)
// > decimalToOctal(8)
// 10
// > decimalToOctal(65)
// 101
// > decimalToOctal(216)
// 330
// > decimalToOctal(512)
// 1000

View File

@ -22,6 +22,11 @@ function hexToDecimal (hexNum) {
}, 0) }, 0)
} }
// test cases export { hexToInt, hexToDecimal }
console.log(hexToDecimal('5DE9A')) // 384666
console.log(hexToDecimal('3D')) // 61
// > hexToDecimal('5DE9A'))
// 384666
// > hexToDecimal('3D'))
// 61

View File

@ -11,4 +11,8 @@ function hexStringToRGB (hexString) {
return obj return obj
} }
console.log(hexStringToRGB('ffffff')) export { hexStringToRGB }
// > hexStringToRGB('ffffff')
// { r: 255, g: 255, b: 255 }

View File

@ -10,6 +10,10 @@ function octalToDecimal (num) {
return dec return dec
} }
// test cases export { octalToDecimal }
console.log(octalToDecimal(56) === 46)
console.log(octalToDecimal(2365) === 1269) // > octalToDecimal(56)
// 46
// > octalToDecimal(2365)
// 1269

View File

@ -12,5 +12,10 @@ function RGBToHex (r, g, b) {
return `#${toHex(r)}${toHex(g)}${toHex(b)}` return `#${toHex(r)}${toHex(g)}${toHex(b)}`
} }
console.log(RGBToHex(255, 255, 255) === '#ffffff') export { RGBToHex }
console.log(RGBToHex(255, 99, 71) === '#ff6347')
// > RGBToHex(255, 255, 255)
// '#ffffff'
// > RGBToHex(255, 99, 71)
// '#ff6347'

View File

@ -33,6 +33,4 @@ function romanToDecimal (romanNumber) {
return sum return sum
} }
console.log(romanToDecimal('XXIIVV')) export { romanToDecimal }
console.log(romanToDecimal('MDCCCIV'))
console.log(romanToDecimal('XXIVI'))