Challenge: Define a function select which takes two arguments, an array and a callback function. The callback function can be considered a test in that it will return either true or false depending on the input. select will iterate/loop through the array and pass each array element to the callback as an argument.
If the callback called on a certain value returns true, the original value (the input to the callback) is pushed to a new array. select will return this new array.
My apologies for not showing code. I am a newb. Currently this question is in a learning course i'm doing, this is a tough one been stuck on for hours. I really don't know what I am doing with this one. Was going so well till I hit a wall in this callbacks module. My code is below for what it's worth but this is one of many solutions I've tried (all wrong unfortunately so I would ignore it if anything).
function select(checkArray, func) { let outputArray = [] checkArray.forEach(function(element) { if(element%2==0) return outputArray.push(element) } } // UNCOMMENT THESE LINES TO CHECK YOUR WORK // const arr = [1, 2, 3, 4, 5]; // const isEven = n => n % 2 === 0; // console.log(select(arr, isEven)); // should log: [2, 4] https://stackoverflow.com/questions/67067214/how-to-solve-the-following-callback-challenge-in-js-ive-tried-the-map-method April 13, 2021 at 08:23AM
没有评论:
发表评论