-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path33-arithGeoII.js
More file actions
31 lines (29 loc) · 1.59 KB
/
33-arithGeoII.js
File metadata and controls
31 lines (29 loc) · 1.59 KB
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
// STEPS:
// 1. Create 2 test variables. The first test case to test the difference b/w the 1st element and the 2nd element. The second test case to test the quotient of the 2nd element divided by the 1st element.
// 2. Create 2 boolean testers and set them to true. These will allow us to test all of the #s in the array, as opposed to just returning "true" after performing the operation on the 1st element of the array / 1st iteration of the for-loop.
// 3. Create a for loop to run thru each element of the array. FOR EACH ELEMENT...
// A. Test the arithmetic test variable. Use !=, so that you can set the applicable boolean-tester to false if the condition is fulfilled.
// B. Test the geometric test variable. Again, use != so that you can set the geometic boolean tester to false if the condition is fulfilled.
// 4. Create a final If-statement to output the return values.
//CODE:
function ArithGeoII(arr) {
var difference = arr[1] - arr[0];
var ratio = arr[1] / arr[0];
var aTester = true;
var gTester = true;
for (var i = 0; i < arr.length - 1; i++){ // Need to make sure you cut out of the for-loop one step early (hence the arr.length - 1) because we are adding 1 to i in our element test cases. If we tried to take the element after the last element, such element doesn't exist so the test would fail every time.
if (arr[i] + difference != arr[i+1]){
aTester = false;
}
if (arr[i+1] / ratio != arr[i]){
gTester = false;
}
}
if (aTester == true){
return "Arithmetic";
} else if (gTester == true){
return "Geometric";
} else {
return -1;
}
}