forked from gideo/CodeWars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6kyu_BasicEncryption.js
More file actions
22 lines (17 loc) · 842 Bytes
/
Copy path6kyu_BasicEncryption.js
File metadata and controls
22 lines (17 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 6 kyu - Basic Encryption
// Description:
// The most basic encryption method is to map a char to another char by a certain math rule.
// Because every char has an ASCII value, we can manipulate this value with a simple math
// expression. For example 'a' + 1 would give us 'b', because 'a' value is 97 and 'b' value is 98.
// You will need to write a method which does exactly that -
// get a string as text and an int as the rule of manipulation, and should return encrypted
// text. for example:
// encrypt("a",1) = "b"
// Full ascii table is used on our question (256 chars) - so 0-255 are the valid values.
// Good luck.
let encrypt = function(str, n) {
return str.replace(/./g, a => {
let temp = a.charCodeAt(0) + n;
return temp>255 ? String.fromCharCode(temp%256) : String.fromCharCode(temp);
})
}