Skip to content

Commit c35b20a

Browse files
committed
DecimalToHex
- Add test file DecimalToHex.test.js - Add export to DecimalToHex.js - Remove console.log from DecimalToHex.js
1 parent c0fa45c commit c35b20a

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

Conversions/DecimalToHex.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function intToHex (num) {
1+
function intToHex(num) {
22
switch (num) {
33
case 10: return 'A'
44
case 11: return 'B'
@@ -10,7 +10,7 @@ function intToHex (num) {
1010
return num
1111
}
1212

13-
function decimalToHex (num) {
13+
function decimalToHex(num) {
1414
const hexOut = []
1515
while (num > 15) {
1616
hexOut.unshift(intToHex(num % 16))
@@ -19,6 +19,4 @@ function decimalToHex (num) {
1919
return intToHex(num) + hexOut.join('')
2020
}
2121

22-
// test cases
23-
console.log(decimalToHex(999098) === 'F3EBA')
24-
console.log(decimalToHex(123) === '7B')
22+
export { decimalToHex }

Conversions/test/DecimalToHex.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { decimalToHex } from '../DecimalToHex'
2+
3+
describe('DecimalToHex', () => {
4+
it('expects to return correct hexadecimal value', () => {
5+
expect(decimalToHex(255)).toBe('FF')
6+
})
7+
8+
it('expects to return correct hexadecimal value, matching (num).toString(16)', () => {
9+
expect(decimalToHex(32768)).toBe((32768).toString(16).toUpperCase())
10+
})
11+
12+
it('expects to not handle negative numbers', () => {
13+
expect(decimalToHex(-32768)).not.toBe((-32768).toString(16).toUpperCase())
14+
})
15+
})

0 commit comments

Comments
 (0)