/**Returns new collection using mapFct to produce new elements */
function mapElements(elements, mapFct)
Let newElements = new dynamic (growable) array
Iterate over collection "elements" (let "nextElement" be the iteration variable)
Let newElement = mapFct(nextElement)
Add newElement to newElements
Return newElements
Pseudocode:
/**Returns new collection using mapFct to produce new elements */
function mapElements(elements, mapFct) {
let newElements = [];
for (let nextElem of elements) {
let newElem = mapFct(nextElem);
newElements.add(newElem);
}
return newElements
}
//-------------------------------------------//Testsvar elements, squareFct, lengthFct, result;
elements = [2, 3, 4];
squareFct = (x) => x * x;
println("Data: " + elements);
println("Squares: " + mapElements(elements, squareFct));
elements = ['Asha', 'Acheampong', 'Mo'];
lengthFct= (str) => str.length;
println("\nData: " + elements);
println("Lengths: " + mapElements(elements, lengthFct));