From 31d5467ada845dca245a6c49e0f9330d8a6f0dd5 Mon Sep 17 00:00:00 2001 From: Alex Brown Date: Sat, 20 Oct 2018 15:35:49 -0500 Subject: [PATCH 1/5] added DecimalToHex --- Conversions/DecimalToHex.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Conversions/DecimalToHex.js diff --git a/Conversions/DecimalToHex.js b/Conversions/DecimalToHex.js new file mode 100644 index 0000000000..f95649b2ab --- /dev/null +++ b/Conversions/DecimalToHex.js @@ -0,0 +1,19 @@ +function intToHex(num){ + switch(num){ + case 10: return "A"; + case 11: return "B"; + case 12: return "C"; + case 13: return "D"; + case 14: return "E"; + case 15: return "F"; + } + return num; +} +function decimalToHex(num){ + let hex_out = []; + while(num > 15) { + hex_out.push(intToHex(num%16)); + num = Math.floor(num / 16); + } + return hex_out.join(""); +} From f44fdc7dd016de2ef1be64df79bd579015fa1967 Mon Sep 17 00:00:00 2001 From: Alex Brown Date: Sun, 21 Oct 2018 18:57:20 -0500 Subject: [PATCH 2/5] added luhn's checksum algorithm --- Checksums/luhn.js | 16 ++++++++++++++++ README.md | 7 +++++++ 2 files changed, 23 insertions(+) create mode 100644 Checksums/luhn.js diff --git a/Checksums/luhn.js b/Checksums/luhn.js new file mode 100644 index 0000000000..486f57e970 --- /dev/null +++ b/Checksums/luhn.js @@ -0,0 +1,16 @@ +function luhn(card_number) { + var sum_of_odds = 0; + var sum_of_evens = 0; + for (var i = 15; i > 0; i-=2) { + sum_of_odds += parseInt(card_number[i]); + } + for (var i = 14; i > -1; i-=2) { + var current_number = parseInt(card_number[i]); + if (parseInt(card_number[i])*2 > 9) { + sum_of_evens += (i*2)-9; + } else { + sum_of_evens += i*2; + } + } + return 0 === ((sum_of_odds + sum_of_evens) % 10); +} diff --git a/README.md b/README.md index ebbfdd0ed9..adcfe321be 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,13 @@ In cryptography, a **transposition cipher** is a method of encryption by which t Mathematically a bijective function is used on the characters' positions to encrypt and an inverse function to decrypt. ###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher) +---------------------------------------------------------------------------------------------------------------------- + +## Checksums + +### Luhn's +The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in the United States, Canadian Social Insurance Numbers, Israel ID Numbers and Greek Social Security Numbers. It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960. +###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher) [bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort [bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort [bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort" From 3e47ca62c0bf88a465bf7ac3da75af5e2002dcd9 Mon Sep 17 00:00:00 2001 From: Alex Brown Date: Tue, 23 Oct 2018 13:25:13 -0500 Subject: [PATCH 3/5] deleted checksums --- Checksums/luhn.js | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 Checksums/luhn.js diff --git a/Checksums/luhn.js b/Checksums/luhn.js deleted file mode 100644 index 486f57e970..0000000000 --- a/Checksums/luhn.js +++ /dev/null @@ -1,16 +0,0 @@ -function luhn(card_number) { - var sum_of_odds = 0; - var sum_of_evens = 0; - for (var i = 15; i > 0; i-=2) { - sum_of_odds += parseInt(card_number[i]); - } - for (var i = 14; i > -1; i-=2) { - var current_number = parseInt(card_number[i]); - if (parseInt(card_number[i])*2 > 9) { - sum_of_evens += (i*2)-9; - } else { - sum_of_evens += i*2; - } - } - return 0 === ((sum_of_odds + sum_of_evens) % 10); -} From 97ac1c3d248f52474d6957a8e3cdd7a93aff1961 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 24 Feb 2019 15:00:42 -0600 Subject: [PATCH 4/5] fixed and added test cases --- Conversions/DecimalToHex.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Conversions/DecimalToHex.js b/Conversions/DecimalToHex.js index f95649b2ab..ead26ccebe 100644 --- a/Conversions/DecimalToHex.js +++ b/Conversions/DecimalToHex.js @@ -9,11 +9,16 @@ function intToHex(num){ } return num; } + function decimalToHex(num){ let hex_out = []; while(num > 15) { hex_out.push(intToHex(num%16)); num = Math.floor(num / 16); } - return hex_out.join(""); + return intToHex(num) + return hex_out.join(""); } + +// test cases +console.log(decimalToHex(999098) === "F3EBA"); +console.log(decimalToHex(123) === "7B"); From 8a356cd839e0191744b4372e99c21790c1f66664 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 24 Feb 2019 15:06:59 -0600 Subject: [PATCH 5/5] fixed alert --- Conversions/DecimalToHex.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Conversions/DecimalToHex.js b/Conversions/DecimalToHex.js index ead26ccebe..db5ee84c24 100644 --- a/Conversions/DecimalToHex.js +++ b/Conversions/DecimalToHex.js @@ -16,7 +16,7 @@ function decimalToHex(num){ hex_out.push(intToHex(num%16)); num = Math.floor(num / 16); } - return intToHex(num) + return hex_out.join(""); + return intToHex(num) + hex_out.join(""); } // test cases