运行 ❯
获取您
自己
的网站
×
更改方向
保存代码
更改主题,深色/浅色
前往空间
<!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>