mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +08:00
DecimalToHex
- Add test file DecimalToHex.test.js - Add export to DecimalToHex.js - Remove console.log from DecimalToHex.js
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
function intToHex (num) {
|
function intToHex(num) {
|
||||||
switch (num) {
|
switch (num) {
|
||||||
case 10: return 'A'
|
case 10: return 'A'
|
||||||
case 11: return 'B'
|
case 11: return 'B'
|
||||||
@ -10,7 +10,7 @@ function intToHex (num) {
|
|||||||
return num
|
return num
|
||||||
}
|
}
|
||||||
|
|
||||||
function decimalToHex (num) {
|
function decimalToHex(num) {
|
||||||
const hexOut = []
|
const hexOut = []
|
||||||
while (num > 15) {
|
while (num > 15) {
|
||||||
hexOut.unshift(intToHex(num % 16))
|
hexOut.unshift(intToHex(num % 16))
|
||||||
@ -19,6 +19,4 @@ function decimalToHex (num) {
|
|||||||
return intToHex(num) + hexOut.join('')
|
return intToHex(num) + hexOut.join('')
|
||||||
}
|
}
|
||||||
|
|
||||||
// test cases
|
export { decimalToHex }
|
||||||
console.log(decimalToHex(999098) === 'F3EBA')
|
|
||||||
console.log(decimalToHex(123) === '7B')
|
|
||||||
|
15
Conversions/test/DecimalToHex.test.js
Normal file
15
Conversions/test/DecimalToHex.test.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import { decimalToHex } from '../DecimalToHex'
|
||||||
|
|
||||||
|
describe('DecimalToHex', () => {
|
||||||
|
it('expects to return correct hexadecimal value', () => {
|
||||||
|
expect(decimalToHex(255)).toBe('FF')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return correct hexadecimal value, matching (num).toString(16)', () => {
|
||||||
|
expect(decimalToHex(32768)).toBe((32768).toString(16).toUpperCase())
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to not handle negative numbers', () => {
|
||||||
|
expect(decimalToHex(-32768)).not.toBe((-32768).toString(16).toUpperCase())
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user