Skip to content

Commit

Permalink
Hex Hash Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
PheRum committed Jun 20, 2024
1 parent a62dfaf commit 12bf1fd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
15 changes: 15 additions & 0 deletions 7_kyu/Hex Hash Sum/README.md
Original file line number Diff line number Diff line change
@@ -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
```
13 changes: 13 additions & 0 deletions 7_kyu/Hex Hash Sum/index.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
8 changes: 8 additions & 0 deletions 7_kyu/Hex Hash Sum/index.ts
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down

0 comments on commit 12bf1fd

Please sign in to comment.