forked from exercism/DEPRECATED.javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
28 lines (24 loc) · 675 Bytes
/
example.js
File metadata and controls
28 lines (24 loc) · 675 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'use strict';
var LETTERS = 'abcdefghijklmnopqrstuvwxyz';
var REVERSED_LETTERS = 'zyxwvutsrqponmlkjihgfedcba';
function insertSpacing(s, interval) {
var matcher = new RegExp(".{1," + interval + "}", "g");
return s.match(matcher).join(' ');
}
function invert(character) {
/*jshint validthis: true */
if (character.match(/\d/)) {
this.push(character);
} else {
this.push(LETTERS[REVERSED_LETTERS.indexOf(character)]);
}
}
module.exports = {
encode: function (s) {
var encoded = '';
var characters = [];
s.toLowerCase().split('').forEach( invert, characters );
encoded = insertSpacing(characters.join(''), 5);
return encoded;
}
};