-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterFrequencies.js
33 lines (28 loc) · 933 Bytes
/
filterFrequencies.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
const filterFrequencies = (frequencyArray, lowerLimit = 40, upperLimit = 1000) => {
if(!Array.isArray(frequencyArray)) {
throw new Error("Argument Error: first argument must be an array")
}
return frequencyArray.map((frequency) => {
if(frequency < 0) {
throw new Error("Value Error: frequencies must be above 0")
} else if(!isFloatOrInteger(frequency)) {
throw new Error("Argument Error: frequencies must only be integers or floating points")
}
return filterFrequency(frequency, lowerLimit, upperLimit)
})
}
const isFloatOrInteger = (value) => {
if(typeof value == 'number' && !isNaN(value)) {
return true
}
return false
}
const filterFrequency = (frequency, lowerLimit, upperLimit) => {
if(frequency < lowerLimit) {
return lowerLimit
} else if (frequency > upperLimit) {
return upperLimit
}
return frequency
}
module.exports = filterFrequencies;