From c35b20a4cebbc1c1a4d13b59fb495cf99665f531 Mon Sep 17 00:00:00 2001 From: TrasherDK Date: Sun, 3 Oct 2021 12:11:34 +0200 Subject: [PATCH] DecimalToHex - Add test file DecimalToHex.test.js - Add export to DecimalToHex.js - Remove console.log from DecimalToHex.js --- Conversions/DecimalToHex.js | 8 +++----- Conversions/test/DecimalToHex.test.js | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 Conversions/test/DecimalToHex.test.js diff --git a/Conversions/DecimalToHex.js b/Conversions/DecimalToHex.js index 5b130de9a..4c40a42c2 100644 --- a/Conversions/DecimalToHex.js +++ b/Conversions/DecimalToHex.js @@ -1,4 +1,4 @@ -function intToHex (num) { +function intToHex(num) { switch (num) { case 10: return 'A' case 11: return 'B' @@ -10,7 +10,7 @@ function intToHex (num) { return num } -function decimalToHex (num) { +function decimalToHex(num) { const hexOut = [] while (num > 15) { hexOut.unshift(intToHex(num % 16)) @@ -19,6 +19,4 @@ function decimalToHex (num) { return intToHex(num) + hexOut.join('') } -// test cases -console.log(decimalToHex(999098) === 'F3EBA') -console.log(decimalToHex(123) === '7B') +export { decimalToHex } diff --git a/Conversions/test/DecimalToHex.test.js b/Conversions/test/DecimalToHex.test.js new file mode 100644 index 000000000..53cd88473 --- /dev/null +++ b/Conversions/test/DecimalToHex.test.js @@ -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()) + }) +})