-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringCalculator.js
53 lines (49 loc) · 1018 Bytes
/
StringCalculator.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
45
46
47
48
49
50
51
52
53
import Generator from "./Generator.js";
/**
* Author James Torkornoo
* This class initializes an object that takes a string and returns the sum using a Generator
* @export
* @class StringCalculator
*/
export default class StringCalculator {
constructor(str) {
this.sum = 0;
this.numArray;
this.negNumArray;
this.str = str;
this.arrGen = new Generator(str);
}
/**
*
*
* @memberof StringCalculator
*/
setArray() {
this.arrGen.convertString();
this.numArray = this.arrGen.finalNumArray;
}
/**
*
*
* @memberof StringCalculator
*/
add() {
this.setArray();
this.sum = this.numArray
.filter((option) => {
return option <= 1000 ? option : 0;
})
.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 0);
}
/**
*
*
* @return {*}
* @memberof StringCalculator
*/
totalNum() {
return `\nThis is the sum of all the number(s) in the string ${this.sum}`;
}
}