Created
April 17, 2014 22:29
-
-
Save rvbsanjose/11015118 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using the JavaScript language, have the function ArithGeoII(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. | |
// Input = 5,10,15 Output = "Arithmetic" | |
// Input = 2,6,18,54 Output = "Geometric" | |
// Input = 2,4,16,24 Output = -1 | |
// Input = 2,4,6,14 Output = -1 | |
function ArithGeoII( arr ) { | |
var math; | |
var number = [ (arr[1] - arr[0]), (arr[1] / arr[0]) ]; | |
for ( var i = 0; i < arr.length-2; i++ ) { | |
math = -1; | |
if ( (number[0] + arr[i+1]) == arr[i+2] ) | |
math = 'Arithmetic'; | |
if ( (number[1] * arr[i+1]) == arr[i+2] ) | |
math = 'Geometric'; | |
} | |
return math; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment