-
-
Save rjbernaldo/11029591 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // User stories for Group Project. | |
| // 1. I want a program that can take any number of integers and add them together | |
| // 2. I want a program that can take any number of integers and calculate their mean (average). | |
| // 3. I want a program that can take any number of integers and find their median number. | |
| // HINT: The name of these "programs" should be: sum, mean, median. | |
| //--------------------- | |
| //PseudoCode | |
| //1. make variable named "sum", take array of integers, produce sum of total. | |
| //2. make variable name "mean", take array of integers, take sum of integers, divide by # of integers | |
| //3. make variable named "median", IF even -> take array of integers, take # of intergers, divide by 2, call result, use result as position in original array to find median. | |
| //If odd -> take array of integers, take # of intergers, divide by 2, call result, round up on result, use result as position in original array to find median. | |
| //-------------------- | |
| //1. | |
| function sum(collection) { | |
| total_sum = 0; | |
| for (var i = 0; i < collection.length; i++) { | |
| total_sum += collection[i]; | |
| } | |
| return total_sum; | |
| } | |
| //2. | |
| function mean(collection) { | |
| total_sum = 0; | |
| for (var i = 0; i < collection.length; i++) { | |
| total_sum += collection[i]; | |
| } | |
| return total_sum / collection.length; | |
| } | |
| //3. | |
| function median(collection) { | |
| collection.sort(); | |
| var tempNum = Math.round(collection.length / 2); | |
| if (collection.length % 2 !== 0) { | |
| return collection[tempNum - 1]; | |
| } else { | |
| var average = (collection[tempNum] + collection[tempNum - 1]) / 2; | |
| return average; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment