Question

In: Computer Science

Using Javascript, complete the following /************************************************************************** * * Array callback filtering * **************************************************************************/ /** * Write...

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) => {

};

Solutions

Expert Solution

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;

}

};


Related Solutions

using javascript and without using javascript sort. Sort an array from lowest to highest const x...
using javascript and without using javascript sort. Sort an array from lowest to highest const x = [201,28,30,-5] ​function sort(array){ // put your code here return //the answer } ​ sort(x) // output: [-5,28,30,201]
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
Write a function in JAVASCRIPT that accepts two arguments (a string array and a character). The...
Write a function in JAVASCRIPT that accepts two arguments (a string array and a character). The function will check each character of the strings in the array and removes the character, regardless of case. If the first letter of the string is removed the next letter must be capitalized. Your function would return the result as one string, where each element of array is separated by comma. E.G. function([“John”,”Hannah”,”Saham”], “h”); // returns ‘Jon,Anna,Saam Use of any built in string function...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Write a complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output...
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output of longestMorseCodeWords should be an array of the strings that were passed in, but ordered by the length of their Morse Code equivalent in descending order. If the length of Morse Code is equal, order the words alphabetically.For convenience, the full table for the 26 letters of the English alphabet is given below: [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] let words = ["gin", "zen", "gig", "msg"] longestMorseCodeWords(words) The Morse...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Use of any built in string functions or built in array functions is not allowed, Any help would be much appreciated
JavaScript Write out a function that takes in array of numbers. You do not need to...
JavaScript Write out a function that takes in array of numbers. You do not need to check if each item of the array is a number, assume this has already been done. Create a new array using the map function that that the original item and adds 4 to it. Then iterate through the new array and log each item to the console. If you do not use map it will be counted wrong. Call your function with the following...
Javascript 1. Write a function which receives four arguments (array, start, end, delimiter). Copy the array...
Javascript 1. Write a function which receives four arguments (array, start, end, delimiter). Copy the array values from the start to end index separated by delimiter\'s value. function([1,2,3,4],1,3,'*') // returns 2*3*4 as string 2. Write a function to change the case of all the characters in string based on their position which matches the value of the pos parameter passed to function(str, pos [even|odd]). Example: function(‘abCd’, ‘odd’) // returns Abcd 3 Write a function which receives two arguments (array, oddOrEven)...
Javascript array of objects: I'm trying to get the input into an array of objects, and...
Javascript array of objects: I'm trying to get the input into an array of objects, and then display the information in a table using a for loop, but I don't think my path is right <!DOCTYPE html> <head> <title>Form</title> <meta charset="utf-8" /> <style media="screen"> h1 { text-align: center; } div { background-color: #pink; border: 2px solid green; padding: 15px; margin: 65px; } </style> <script> var list = []; total = 0; text = ""; function Product(item, quantity, costs){ this.item =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT