-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
38 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters