Write a function named rejectElements that returns a new collection containing all elements in the passed collection by rejecting those that yield "true" using the passed reject function.
This function is the inverse of the problem "Filter Elements".
Function header: function rejectElements(elements, rejectFct)
As an example let's say the old collection passed to rejectElements is [10, 11, 12, 13, 14, 15] and rejectFct outputs true when the input value is an odd number. The expected output for rejectElements would be [10, 12, 14].
Let's say we tweak the example so the rejectFct outputs true when the input value is <= 12. The expected output for rejectElements would then be [13, 14, 15].
The passed rejectFct is invoked with input that is an old element (from the passed elements array) and it outputs true when an element should be rejected from the result.
Example Hand Calculation #1:
elements = [10, 11, 12, 13, 14, 15];
//fct returns true if x is odd
rejectFct = (x) => x % 2 == 1;
rejectElements(elements, rejectFct) outputs [10, 12, 14]
Example Hand Calculation #2:
elements = [10, 11, 12, 13, 14, 15];
//fct returns true if x is equal or less than 12
rejectFct = (x) => x <= 12;
rejectElements(elements, rejectFct) outputs [13, 14, 15]
Example Hand Calculation #3:
elements = ['Asha', 'Acheampong', 'Mo'];
//fct returns true if length of string is less than 5
rejectFct = (nm) => nm.length < 5;
rejectElements(elements, rejectFct) outputs ['Acheampong'];
/**Returns new collection by rejecting elements in passed collection per rejectFct*///This is basically inverse of filterElements (another problem in our problem set). Therefore, we can reuse "selectElements", by inverting the rejectFct.
function rejectElements(elements, rejectFct) {
Let invertedFct = (elem) => !rejectFct(elem)
return selectElements(elements, invertedFct)
}
Pseudocode:
/**Returns new collection by rejecting elements in passed collection per rejectFct*/
function rejectElements(elements, rejectFct) {
let invertedFct = (elem) => !rejectFct(elem)
return selectElements(elements, invertedFct)
}
//-------------------------------------------------//Previous work
function selectElements(elements, selectFct) {
let newElements = Array.newDynamic();
for (let nextElem of elements) {
if (selectFct(nextElem))
newElements.add(nextElem);
}
return newElements
}
//-------------------------------------------//Testsvar elements, rejectFct, result;
elements = [10, 11, 12, 13, 14, 15];
//fct returns true if x is odd
rejectFct = (x) => x % 2 == 1;
result = rejectElements(elements, rejectFct);
println('Data: ' + elements);
println('Rejecting odd: ' + result);
elements = [10, 11, 12, 13, 14, 15];
//fct returns true if x is equal or less than 12
rejectFct= (x) => x <= 12;
result = rejectElements(elements, rejectFct);
println('\nData: ' + elements);
println('Rejecting <= 12: ' + result);
elements = ['Asha', 'Acheampong', 'Mo'];
//fct returns true if length of string is less than 5
rejectFct= (nm) => nm.length < 5;
result = rejectElements(elements, rejectFct)
println('\nData: ' + elements);
println('Rejecting Shorter Strings: ' + result);