Created
March 4, 2012 01:09
-
-
Save lzcabrera/1969725 to your computer and use it in GitHub Desktop.
Amplitude of An Array
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
var A = [-100, 2, 44, 0, 39, 5000010]; | |
function amplitude(A){ | |
var n = A.length; | |
var i; | |
var min_val; | |
var max_val; | |
var allowed_min = 0; | |
var allowed_max = 5000000; | |
var max_elements = 3; | |
console.log('n: '+n); | |
if(n == 0 || n > max_elements) | |
return -1; | |
for(i = 0; i < n; i++){ | |
// detect if value is min value allowed | |
if(A[i] <= allowed_min){ | |
A[i] = allowed_min; | |
console.log('resetting val min'); | |
} | |
// detect if value is max value allowed | |
if(A[i] >= allowed_max){ | |
A[i] = allowed_max; | |
console.log('al max set'); | |
} | |
if(i==0){ | |
console.log('setting defaults'); | |
min_val = A[i]; | |
max_val = A[i]; | |
} | |
if(A[i] < min_val){ | |
console.log('min_val '+A[i]+ ' index: '+i); | |
min_val = A[i]; | |
} | |
if(A[i] > max_val){ | |
console.log('max_val '+A[i]+ ' index: '+i); | |
max_val = A[i]; | |
} | |
} | |
return (max_val - min_val); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment