Quick Index
Steps



Demo


Here is a demo

Problem Statement


Write a function named selectElements that returns a new collection containing all elements in the passed (old) collection where the passed select function outputs true when invoked

Function header: function selectElements(elements, selectFct)

As an example let's say the old collection passed to selectElements is [10, 11, 12, 13, 14, 15] and selectFct outputs true when the input value is an odd number. The expected output for selectElements would be [11, 13, 15].

Let's say we tweak the example so the selectFct outputs true when the input value is <= 12. The expected output for selectElements would then be [10, 11, 12].

The passed selectFct is invoked with input that is an old element (from the passed elements array) and it outputs true when an element should be selected (kept).

Example Hand Calculation #1:

elements = [10, 11, 12, 13, 14, 15];
//fct returns true if x is odd
selectFct = (x) => x % 2 == 1;
selectElements(elements, selectFct) outputs [11, 13, 15]


Example Hand Calculation #2:

elements = [10, 11, 12, 13, 14, 15];
//fct returns true if x is equal or less than 12
selectFct = (x) => x <= 12;
selectElements(elements, selectFct) outputs [10, 11, 12]


Example Hand Calculation #3:

elements = ['Asha', 'Acheampong', 'Mo'];
//fct returns true if length of string is less than 5
selectFct = (nm) => nm.length < 5;
selectElements(elements, selectFct) outputs ['Asha', 'Mo'];






































































































Hints


General Hints:



Specific Hints:







































































































Solution


Algorithm:

/**Returns new collection by selecting elements in passed collection per selectFct*/
function selectElements(elements, selectFct)
	Let newElements = new dynamic (growable) array
	Iterate over collection "elements" (let "nextElement" be the iteration variable)
		If (selectFct(nextElement))
			Add nextElement to newElements
	Return newElements


Pseudocode:

/**Returns new collection by selecting elements in passed collection per selectFct*/
function selectElements(elements, selectFct) {
	let newElements = Array.newDynamic();
	for (let nextElem of elements) {
		if (selectFct(nextElem))
			newElements.add(nextElem);
	}
	return newElements
}

//-------------------------------------------
//Tests
var elements, selectFct, result;

elements = [10, 11, 12, 13, 14, 15];
//fct returns true if x is odd
selectFct = (x) => x % 2 == 1;
result = selectElements(elements, selectFct);
println('Data: ' + elements);
println('Odd Numbers: ' + result);

elements = [10, 11, 12, 13, 14, 15];
//fct returns true if x is equal or less than 12
selectFct = (x) => x <= 12;
result = selectElements(elements, selectFct);
println('\nData: ' + elements);
println('Numbers <= 12: ' + result);

elements = ['Asha', 'Acheampong', 'Mo'];
//fct returns true if length of string is less than 5
selectFct = (nm) => nm.length < 5;
result = selectElements(elements, selectFct)
println('\nData: ' + elements);
println('Shorter Strings: ' + result);