|
| 1 | +/*************************************************************************************** |
| 2 | +* * |
| 3 | +* CODERBYTE BEGINNER CHALLENGE * |
| 4 | +* * |
| 5 | +* Arith Geo * |
| 6 | +* Using the JavaScript language, have the function ArithGeo(arr) take the array of * |
| 7 | +* numbers stored in arr and return the string "Arithmetic" if the sequence follows * |
| 8 | +* an arithmetic pattern or return "Geometric" if it follows a geometric pattern. If * |
| 9 | +* the sequence doesn't follow either pattern return -1. An arithmetic sequence is * |
| 10 | +* one where the difference between each of the numbers is consistent, where as in a * |
| 11 | +* geometric sequence, each term after the first is multiplied by some constant or * |
| 12 | +* common ratio. Arithmetic example: [2, 4, 6, 8] and Geometric * |
| 13 | +* example: [2, 6, 18, 54]. Negative numbers may be entered as parameters, 0 will not * |
| 14 | +* be entered, and no array will contain all the same elements. * |
| 15 | +* * |
| 16 | +* SOLUTION * |
| 17 | +* To check for arithmetic pattern, start by getting the difference between the first * |
| 18 | +* two number. Then loop thru array starting in position 2 and subtract the previous * |
| 19 | +* number. If the difference is equal to the initial difference then you have an * |
| 20 | +* arithmetic pattern so return arithmetic. Next repeat by getting initial difference * |
| 21 | +* by dividing the first and second numbers. Loop through array starting in position * |
| 22 | +* 2 and compare the current number divided by previous number. If difference is * |
| 23 | +* equal to the initial number then you have a geometric pattern. Else return -1. * |
| 24 | +* * |
| 25 | +* Steps for solution * |
| 26 | +* 1) set flags for both patterns to True * |
| 27 | +* 2) Loop thru array starting in position 2 and compare difference between current * |
| 28 | +* number and the previous number to see if it is equal to initial difference * |
| 29 | +* 3) If aritimetic pattern exists return arithmetic else check for geometric * |
| 30 | +* 4) Get difference between second numvber divided by first number * |
| 31 | +* 5) Loop thru array starting in position 2 and compare difference between current * |
| 32 | +* number and the previous to see if it is equal to the intial difference * |
| 33 | +* 6) If geometric pattern exists return geometric * |
| 34 | +* 7) Else return -1 * |
| 35 | +* * |
| 36 | +***************************************************************************************/ |
| 37 | + |
| 38 | +function ArithGeo(arr) { |
| 39 | + |
| 40 | + var arithFlag = true, geoFlag = true; |
| 41 | + var diff = arr[1] - arr[0]; |
| 42 | + |
| 43 | + for (var i = 2; i < arr.length; i++) { |
| 44 | + if ((arr[i] - arr[i-1]) !== diff) { |
| 45 | + arithFlag = false; |
| 46 | + } |
| 47 | + } |
| 48 | + if (arithFlag) { |
| 49 | + return "Arithmetic"; |
| 50 | + } |
| 51 | + else { // check for geometric pattern |
| 52 | + diff = arr[1] / arr[0]; |
| 53 | + for (var i = 2; i < arr.length; i++) { |
| 54 | + if ((arr[i] / arr[i-1]) !== diff) { |
| 55 | + geoFlag = false; |
| 56 | + } |
| 57 | + } |
| 58 | + if (geoFlag) { |
| 59 | + return "Geometric"; |
| 60 | + } |
| 61 | + else { |
| 62 | + return "-1"; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments