-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.js
70 lines (52 loc) · 2.14 KB
/
utilities.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
60
61
62
63
64
65
66
67
68
69
70
function validateArray(inputArray){
if (typeof (inputObject) === "number") {
// check if Number is not a NaN
if (isNaN(inputObject)) {
throw new Error("Input is NaN.")
}
// check for negative values
if (inputObject < 0) {
throw new Error("Input cannot be negative")
}
// check for integral values only
if ((inputObject % 1) != 0) {
throw new Error("Input needs to be an integral value.")
}
while (inputObject != 0) {
this._internalArray.unshift(inputObject % 10)
inputObject = Math.floor(inputObject / 10)
}
} else if (typeof (inputObject) === "string") {
// check if length is not zero
if (inputObject.length == 0) {
throw new Error("Empty string is not accepted.")
}
// check if every character is a decimal digit
let myRegex = /^[0-9]+$/
if (!myRegex.test(inputObject)) {
throw new Error("String can have decimal numbers only.")
}
for (let index = 0; index < inputObject.length; index++) {
const currentDigit = parseInt(inputObject.charAt(index))
this._internalArray.push(currentDigit)
}
}
else if (Array.isArray(inputObject)) { // IS THIS HOW ITS DONE?
console.log("You sent an Array")
// TODO validate the individual elements of the inputArray and initialize
// the _internalArray
// initialize the member array
this._internalArray = inputObject
} else if (typeof inputObject === "object") { // IS THIS HOW ITS DONE?
console.log("You sent an Object")
// TODO check if this object has getInternalArray() and make a deep copy
// and assign it to local _internalArray
// initialize the member array
this._internalArray = [4, 5, 6]
} else {
console.log("You sent some bullshit!")
throw new Error(`Constuctor of IniniteNumber does not support this data`
+ ` type ${typeof inputObject}`)
}
}
module.exports = {validateArray}