-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy patharith_geo.js
49 lines (43 loc) · 1.47 KB
/
arith_geo.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
/**
* Have the function arithGeo(arr) take the array of numbers stored in arr and
* return the string "Arithmetic" if the sequence follows an arithmetic pattern
* or return "Geometric" if it follows a geometric pattern. If the sequence
* doesn't follow either pattern return -1. An arithmetic sequence is one where
* the difference between each of the numbers is consistent, where as in a
* geometric sequence, each term after the first is multiplied by some constant
* or common ratio. Arithmetic example: [2, 4, 6, 8] and Geometric example: [2,
* 6, 18, 54]. Negative numbers may be entered as parameters, 0 will not be
* entered, and no array will contain all the same elements.
*
* https://www.coderbyte.com/results/bhanson:Arith%20Geo:JavaScript
*
* @param {array} arr
* @return {string} or -1 on failure
*/
function arithGeo(arr) {
if (arr.length === 1 || arr.length === 0) {
return -1;
}
let arithmetic = true;
// test arithmetic
for (let i = 2, diff = arr[1] - arr[0]; i < arr.length; i++) {
if (arr[i] - arr[i - 1] !== diff) {
arithmetic = false;
}
}
if (arithmetic) {
return 'Arithmetic';
}
let geometric = true;
// geometric
for (let i = 2, divisor = arr[1] / arr[0]; i < arr.length; i++) {
if (arr[i] / arr[i - 1] !== divisor) {
geometric = false;
}
}
if (geometric) {
return 'Geometric';
}
return -1;
}
module.exports = arithGeo;