From 12bf1fd76bf50757769ab1ac602c9ab7cbb37d9d Mon Sep 17 00:00:00 2001 From: Valery Malyshev Date: Fri, 21 Jun 2024 00:02:46 +0300 Subject: [PATCH] Hex Hash Sum --- 7_kyu/Hex Hash Sum/README.md | 15 +++++++++++++++ 7_kyu/Hex Hash Sum/index.test.ts | 13 +++++++++++++ 7_kyu/Hex Hash Sum/index.ts | 8 ++++++++ README.md | 4 ++-- 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 7_kyu/Hex Hash Sum/README.md create mode 100644 7_kyu/Hex Hash Sum/index.test.ts create mode 100644 7_kyu/Hex Hash Sum/index.ts diff --git a/7_kyu/Hex Hash Sum/README.md b/7_kyu/Hex Hash Sum/README.md new file mode 100644 index 0000000..e4e4b9b --- /dev/null +++ b/7_kyu/Hex Hash Sum/README.md @@ -0,0 +1,15 @@ +## Hex Hash Sum + +https://www.codewars.com/kata/5ab363ff6a176b29880000dd + +Complete the function that accepts a valid string and returns an integer. + +Wait, that would be too easy! Every character of the string should be converted to the hex value of its ascii code, then the result should be the sum of the numbers in the hex strings (ignore letters). + +**Examples** + +```js +"Yo" ==> "59 6f" ==> 5 + 9 + 6 = 20 +"Hello, World!" ==> 91 +"Forty4Three" ==> 113 +``` diff --git a/7_kyu/Hex Hash Sum/index.test.ts b/7_kyu/Hex Hash Sum/index.test.ts new file mode 100644 index 0000000..0e522ca --- /dev/null +++ b/7_kyu/Hex Hash Sum/index.test.ts @@ -0,0 +1,13 @@ +import { hexHash } from "./index"; + +describe("Tests", () => { + it("Yo", function () { + expect(hexHash("Yo")).toBe(20); + }); + it("Hello, World!", function () { + expect(hexHash("Hello, World!")).toBe(91); + }); + it("Forty4Three", function () { + expect(hexHash("Forty4Three")).toBe(113); + }); +}); diff --git a/7_kyu/Hex Hash Sum/index.ts b/7_kyu/Hex Hash Sum/index.ts new file mode 100644 index 0000000..3a8912d --- /dev/null +++ b/7_kyu/Hex Hash Sum/index.ts @@ -0,0 +1,8 @@ +export function hexHash(code: string): number { + return [...code] + .map((char) => char.charCodeAt(0).toString(16)) + .toString() + .replace(/,|\D/g, "") + .split("") + .reduce((acc, cur) => acc + +cur, 0); +} diff --git a/README.md b/README.md index 38d4571..2a78557 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ ### Katas solved -`Total`: 116 \ +`Total`: 117 \ `8_kyu`: 92 \ -`7_kyu`: 15 \ +`7_kyu`: 16 \ `6_kyu`: 9 \ `5_kyu`: 0 \ `4_kyu`: 0 \