In: Computer Science
Using Javascript, complete the following
/**************************************************************************
*
* Array callback filtering
*
**************************************************************************/
/**
* Write and export a function named "everyEven" which takes an
array and a test
* function for checking individual elements of the array. The
"everyEven"
* function should test the even elements of the array and return
true only
* if at least one of the even elements passes the test.
*
* @param arr An array whose even elements should be tested
* @param test A function which takes as input a single element of
the array
* and returns true or false, such that true means the element
* passed the test and false means it failed
* @return boolean true if at every even-indexed element passes the
test
* function
*
* Example usage:
* everyEven([1, 5, 1, 0, 1], x => x === 1) <-- returns
true
* everyEven([1, 1, 0, 1, 1], x => x === 1) <-- returns
false
*/
export const everyEven = (arr, test) => {
};
/**
* Write and export a function named "someEven" which takes an array
and a test
* function for checking individual elements of the array. The
"someEven"
* function should test the even elements of the array and return
true only
* if at least one of the even elements passes the test.
*
* @param arr An array whose even elements should be tested
* @param test A function which takes as input a single element of
the array
* and returns true or false, such that true means the element
* passed the test and false means it failed
* @return boolean true if at least one even-indexed element passes
the
* test function
*
* Example usage:
* someEven([4, 3, 2, 1, 0], x => x === 3) <-- returns
false
* someEven([1, 0, 1, 0, 1], x => x === 0) <-- returns
false
* someEven([1, 1, 1, 1, 0], x => x === 0) <-- returns
true
* someEven([0, 0, 0, 0, 0], x => x === 0) <-- returns
true
*/
export const someEven = (arr, test) => {
};
/**
* Write and export a function named "filter" which takes an array
and a test
* function for checking individual elements of the array. The
"filter"
* function should test the elements of the array and return true
only
* if all of the odd elements pass the test.
*
* @param arr An array whose elements should be tested
* @param test A function which takes as input a single element of
the array
* and returns true or false, such that true means the element
* passes the test and false means it fails the test
* @return {fail: [], pass: []} an object with two keys: "pass" and
"fail". The value
* of "pass" should be an array containing all the elements of
arr
* which passed the test. The value of "fail" should be an
array
* containing all the elements of arr which failed the test.
*
* Example usage:
* filter(['yes', 'nope', 'maybe', 'yellow'], x => x[0] ===
'y')
* --> { pass: ['yes', 'yellow'], fail: ['nope', 'maybe'] }
* filter([1, 90, 5, 31], x => x % 2 === 1)
* --> { pass: [1, 5, 31], fail: [90] }
*/
export const filter = (arr, test) => {
};
/**
* Write and export a function named "allEvensAreOdd" which takes as
input an
* array and returns true only if all of the even elements in the
array are
* odd numbers. Use the "everyEven" function in this function.
*/
export const allEvensAreOdd = (arr) => {
};
/**
* Write and export a function named "anEvenIsOdd" which takes as
input an
* array and returns true if at least one of the even-indexed
elements in the
* array is an odd number. Use the "someEven" function in this
function.
*/
export const anEvenIsOdd = (arr) => {
};
/**
* Write and export a function named "hasExactly" which takes an
array, a test
* function for checking individual elements of the array, and a
number n.
* The "hasExactly" function should return true only if exactly n
elements
* pass the test. You must use the filter function.
*/
export const hasExactly = (arr, test, n) => {
};
export const everyEven = (arr, test) => {
//for each even index
for(let i = 0; i < arr.length; i+=2){
//get the function value
if(!test(arr[i])){
return false;
}
}
return true;
};
export const someEven = (arr, test) => {
//for each even index
for(let i = 0; i < arr.length; i+=2){
//get the function value
if(test(arr[i])){
return true;
}
}
return false;
};
export const filter = (arr, test) => {
let result = {pass:[], fail :[]};
//for each even index
for(let i = 0; i < arr.length; i++){
//get the function value
if(test(arr[i])){
result.pass.push(arr[i]);
}
else{
result.fail.push(arr[i]);
}
}
return result;
};
export const allEvensAreOdd = (arr) => {
return everyEven(arr, x => x%2==1);
}
export const anEvenIsOdd = (arr) => {
return someEven(arr, x => x%2==1);
}
export const hasExactly = (arr, test, n) => {
let result = filter(arr, test);
if(result.pass.length == n){
return true;
}
else{
return false;
}
};