forked from jfarmer/exercises-js-fundamentals-ii
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunLengthEncode.js
More file actions
39 lines (34 loc) · 1.3 KB
/
Copy pathrunLengthEncode.js
File metadata and controls
39 lines (34 loc) · 1.3 KB
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
29
30
31
32
33
34
35
36
37
38
39
/**
* Given a string that doesn't contain any numbers, returns a run-length
* encoded copy of the input string.
*
* We're exluding numbers to avoid ambiguity. A string like '112211' would
* be encoded as encoded as '212221', which could also mean twenty-one 2s
* followed by twenty-two 1s.
*
* There are ways around this, but focus first on getting a version that
* works strings that doesn't contain numbers.
*
*
* @example
* runLengthEncode('A'); // => '1A'
* runLengthEncode('AB'); // => '1A1B'
* runLengthEncode('Mississippi'); // => '1M1i2s1i2s1i2p1i'
* runLengthEncode('WWWWWWAAAAAAWWWWWWAAAAAABBBBBB'); // => '6W6A6W6A6B'
*
* @param {string} input - A string that doesn't contain numbers
* @returns {string} A run-length encoded copy of the input string
*/
function runLengthEncode(num) {
// This is your job. :)
// Remember, if the code is stumping you, take a step back and
// make sure you can do it by hand.
}
if (require.main === module) {
console.log('Running sanity checks for runLengthEncode:');
console.log(runLengthEncode('WWWWWWAAAAAAWWWWWWAAAAAABBBBBB') === '6W6A6W6A6B');
console.log(runLengthEncode('A') === '1A');
console.log(runLengthEncode('AB') === '1A1B');
console.log(runLengthEncode('Mississippi') === '1M1i2s1i2s1i2p1i');
}
module.exports = runLengthEncode;