Created
March 1, 2021 14:14
-
-
Save bogoreh/85963c046934fc7488624ba8eecea982 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
| var indexOfMinimum = function(array, startIndex) { | |
| // Set initial values for minValue and minIndex, | |
| // based on the leftmost entry in the subarray: | |
| var minValue = array[startIndex]; | |
| var minIndex = startIndex; | |
| // Loop over items starting with startIndex, | |
| // updating minValue and minIndex as needed: | |
| for (var i=minIndex+1; i<array.length; i++){ | |
| if (array[i] < array[minIndex]) | |
| { | |
| minIndex = i; | |
| minValue = array[i]; | |
| } | |
| } | |
| return minIndex; | |
| }; | |
| //declaring array | |
| var array = [18, 6, 66, 44, 9, 22, 14]; | |
| //passing array and variable to function | |
| var index = indexOfMinimum(array, 2); | |
| // For the test array [18, 6, 66, 44, 9, 22, 14], | |
| // the value 9 is the smallest of [..66, 44, 9, 22, 14] | |
| // Since 9 is at index 4 in the original array, | |
| // "index" has value 4 | |
| //check 1 | |
| println("The index of the minimum value of the subarray starting at index 2 is " + index + "." ); | |
| Program.assertEqual(index, 4); | |
| //check 2 | |
| println("The index of the minimum value of the subarray starting at index 0 is " + indexOfMinimum(array, 0) + "." ); | |
| Program.assertEqual(indexOfMinimum(array, 0), 1); | |
| //check 3 | |
| println("The index of the minimum value of the subarray starting at index 5 is " + indexOfMinimum(array,5) + "." ); | |
| Program.assertEqual(indexOfMinimum(array, 5), 6); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment