-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdisk.js
44 lines (36 loc) · 1.04 KB
/
disk.js
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
40
41
42
43
44
class Disk {
constructor({ value, size }) {
this.value = String(value);
this.size = size;
this.grow();
}
grow() {
while(this.value.length < this.size) {
let b = this.value;
let b_array = b.split('');
b_array.reverse();
let new_b = b_array.map(v => v === '1' ? '0' : '1').join('')
this.value = this.value + '0' + new_b;
}
this.value = this.value.substr(0, this.size);
return this.value;
}
checksum() {
let checksum = this.value;
while (checksum.length % 2 === 0) {
let new_val = '';
for (let i = 0; i < checksum.length - 1; i += 2) {
let a = checksum[i];
let b = checksum[i + 1];
if (a === b) {
new_val += '1';
} else {
new_val += '0';
}
}
checksum = new_val;
}
return checksum;
}
}
module.exports = Disk;