-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
59 lines (49 loc) · 1.34 KB
/
main.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
54
55
56
57
58
59
const CONTROL = "//";
const IDEAL_DELIMETER = ",";
/*
* Sanitizes a giving string to be comma separated numbers
* Handlers delimeters of any length and standardizing them to commas
*/
function processString(s) {
let delimeters = [];
const newLineSplit = s.split("\n");
if (s.substr(0, 2) == CONTROL) {
delimeters = newLineSplit[0].substring(2).split(",");
newLineSplit.shift();
}
let processedString = newLineSplit.join("");
for (let i = 0; i < delimeters.length; i++) {
processedString = processedString.replaceAll(
delimeters[i],
IDEAL_DELIMETER
);
}
return processedString;
}
/*
* Adds all numbers within a delimeter separated string
*/
function addString(s) {
let answer = 0;
let negatives = [];
if (!s.length) return answer;
const cleanStringNumbers = processString(s).split(IDEAL_DELIMETER);
for (let i = 0; i < cleanStringNumbers.length; i++) {
// Rough catch all for bad input
if (isNaN(cleanStringNumbers[i]))
throw new Error(`Bad input ${cleanStringNumbers[i]}`);
const number = parseInt(cleanStringNumbers[i]);
if (number < 0) {
negatives.push(number);
}
answer += number;
}
if (negatives.length > 0) {
throw new Error(`Negatives not allowed; ${negatives.toString()}`);
}
return answer;
}
module.exports = {
addString,
processString,
};