Quick Index
function find(elements, compareFct) {
let len = elements.length
let i = 0
while (i < len) {
let nextElem = elements[i]
if (compareFct(nextElem))
return i
i = i + 1
}
return -1
}

function findLast(elements, compareFct) {
let len = elements.length
let i = len - 1
while (i >= 0) {
let nextElem = elements[i]
if (compareFct(nextElem))
return i
i = i - 1
}
return -1
}

function findClosestToEnd(elements, compareFct) {
var left, right, firstIndex, lastIndex, leftDistance, rightDistance;
left = 0;
right = elements.length - 1;
firstIndex = find(elements, compareFct);
lastIndex = findLast(elements, compareFct);
leftDistance = firstIndex - left;
rightDistance = right - lastIndex;
return (leftDistance <= rightDistance)
? firstIndex
: lastIndex;
}

//Try It
let nums, matchFct, matchingIndex, closestToEndIndex;

nums = [5,10,0,10,8,3];
println(nums);
matchFct = (nextNum) => nextNum == 10;
matchingIndex = find(nums, matchFct);
println("Match first (10): " + matchingIndex);
matchingIndex = findLast(nums, matchFct);
println("Match last (10): " + matchingIndex);
closestToEndIndex = findClosestToEnd(nums, matchFct);
println("closestToEndIndex (10): " + closestToEndIndex);
nums = [5,3,10,8,10,3];
println(nums);
closestToEndIndex = findClosestToEnd(nums, matchFct);
println("closestToEndIndex (10): " + closestToEndIndex);

Overview


This challenge consists of three challenge problems.

Hint: First work through the "Examples" and "Problems" pages of this chapter.

Scoring


Scoring is evenly split among the problems (this is typical for all challenges unless otherwise noted).

Reuse 💡


The context for each of these three challenge problems is:


This means that anytime we are working on an algorithm, if we "see" another algo in our "context" that will help, we can "reuse" it -- i.e. "call" it -- like we called "make toast" from "Make Tomato Sandwiches" in the previous chapter.

General Instructions


Template files are provided for you here...

Please put your work for each challenge problem in the appropriate template file and put your work in the part marked "SOLUTION HERE".

It is a good idea to validate your pseudocode solution (see "Walking Through Algorithm" in the chapter examples and problems).

There is [no] compiling or running involved in this challenge (that is coming soon).

More details are provided in the Submitting section at the end of this page.

Special Grading Note 💡


Naturally we want our algorithms to produce the correct result.

But 50% of the scoring on these three challenges will also be based on simplicity -- it may take you multiple iterations (tries) to evolve your algorithm to simplicity.

Challenge 1 -- Write an Algorithm Using Pseudocode to Compute the Product of a Collection of Numbers


Algorithm name and input parameters: computeProduct(numbers)

computeProduct(numbers)
Problem Statement:
Write an algorithm named "computeProduct" to compute the product of a collection of numbers. Return the result.

Given: Variable 'numbers' that holds a collection of numbers.
Sample 1:


Sample 2:
Human Language
Write the human language that solves the problem statement.

Note: this step is optional. You may jump directly to the pseudocode if desired. However, it is good practice.

Because it is optional, you may do it in any human language.
computeProduct(numbers) {
	//SOLUTION HERE








}
Pseudocode
Write the pseudocode that solves the problem statement.
computeProduct(numbers) {
	//SOLUTION HERE









}

Challenge 2 -- Write an Algorithm to Find Last Matching Element in Collection


Algorithm name and input parameters: findLast(elements, compareFct)

findLast(elements, compareFct)
Problem Statement:
Write an algorithm named "findLast" to find the last matching element in a collection named "elements" (provided). Use "compareFct" (provided) to determine if we have a match. Return the index of the match, or "-1" if no match.

Given:
  • Variable 'elements' that holds a collection of objects
  • Variable 'compareFct', when evaluated like this "compareFct(anElem)", returns true if "anElem" is a "match"
Sample 1:

Sample 2:
Human Language
Write the human language that solves the problem statement.

Note: this step is optional. You may jump directly to the pseudocode if desired. However, it is good practice.

Because it is optional, you may do it in any human language.
findLast(elements, compareFct) {
	//SOLUTION HERE








}
Pseudocode
Write the pseudocode that solves the problem statement.
findLast(elements, compareFct) {
	//SOLUTION HERE









}

Challenge 3 -- Write an Algorithm to Find Element Closest to Either End


Algorithm name and input parameters: findClosestToEnd(elements, compareFct)

findClosestToEnd(elements, compareFct)
You must call two other algorithms (sub-algorithms) for this algorithm. You will then use the results from those calls. Hint -- select one algo from "Problems" and one algo from challenge #1 or #2

See tips here... 💡
Problem Statement: Write an algorithm named "findClosestToEnd" to find the matching element that is closest to either end (as described in two examples below). Return the matching index.

If we have elements [5, 10, 0, 10, 8, 3]
And if we look for a match of "10"
Then the result (index) would be 1
(left match is closer to left end then right match is to right end)
left match is 1 from end
right match is 2 from end

If we have elements [5, 0, 10, 3, 8, 10]
And if we look for a match of "10"
Then the result (index) would be 5
(right match is closer to right end then left match is to left end)
left match is 2 from end
right match is 0 from end

If a tie, then "left" index wins, return it.

Given:
More Samples
See more samples here...
Human Language
Write the human language that solves the problem statement.

Note: this step is optional. You may jump directly to the pseudocode if desired. However, it is good practice.

Because it is optional, you may do it in any human language.
findClosestToEnd(elements, compareFct) {
	//SOLUTION HERE








}
Pseudocode
Write the pseudocode that solves the problem statement.
findClosestToEnd(elements, compareFct) {
	//SOLUTION HERE









}

Submitting