Run ❯
Get your
own
website
Result Size:
625 x 565
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to Spaces
<!DOCTYPE html> <html> <body style="text-align: right"> <h1>JavaScript Functions</h1> <h2>Callback Functions</h2> <p id="demo"></p> <script> // Create an Array const myNumbers = [4, 1, -20, -7, 5, 9, -6]; // Call removeNeg with a Callback const posNumbers = removeNeg(myNumbers, (x) => x >= 0); // Display Result document.getElementById("demo").innerHTML = posNumbers; // Remove negative numbers function removeNeg(numbers, callback) { const myArray = []; for (const x of numbers) { if (callback(x)) { myArray.push(x); } } return myArray; } </script> </body> </html>