Loop through our elements until we find a match Return the index of the match
findClosestToEnd(compareFct) { //ALGORITHM HERE }
findClosestToEnd(compareFct) {
firstIndex = get index of first (left) end of array
lastIndex = get index of last (right) end of array
firstMatchingIndex = search for first matching elem (index)
lastMatchingIndex = search for last matching elem (index)
leftDistance = distance from firstIndex to the firstMatchingIndex
rightDistance = distance from lastMatchingIndex to the lastIndex
}
findClosestToEnd(compareFct) { firstIndex = 0 lastIndex = this.length - 1 firstMatchingIndex = search for first matching elem (index) lastMatchingIndex = search for last matching elem (index) leftDistance = firstMatchingIndex - firstIndex rightDistance = lastIndex - lastMatchingIndex }
firstMatchingIndex = search for first matching element (index) using compareFct lastMatchingIndex = search for last matching element (index) using compareFct
static isPrime(n) { let result; if (n <= 1) { result = false; } else { if (n == 2) { result = true; } else { if (n % 2 == 0) { result = false; } else { if (n <= 7) { result = true; } else { result = true let max = Math.sqrt(n) let factor = 3 while (factor <= max) { if (n % factor == 0) { result = false; } factor += 2; } } } } } return result; }
static isPrime(n) { // Special Cases if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; if (n <= 7) return true; let max = Math.sqrt(n) let factor = 3 // "Return" if we find result while (factor <= max) { if (n % factor == 0) return false; factor += 2; } return true; }
static isPrime(n) { let result; if (n <= 1) { result = false; } else { if (n == 2) { result = true; } else { if (n % 2 == 0) { result = false; } else { if (n <= 7) { result = true; } else { result = true let max = Math.sqrt(n) let factor = 3 while (factor <= max) { if (n % factor == 0) { result = false; } factor += 2; } } } } } return result; }
static isPrime(n) { // Special Cases if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; if (n <= 7) return true; let max = Math.sqrt(n) let factor = 3 // "Return" if we find result while (factor <= max) { if (n % factor == 0) return false; factor += 2; } return true; }
z = 1 while (n < 10) print ("n is: " + n) increment n finish()
z = 1;
while (n < 10) {
System.out.println("n is: " + n);
n++;
}
finish();