1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin });
// cat input.txt | node Array_filter.js var lines = [];
rl.on('line', function (line){ lines.push(line); })
rl.on('close', function() { slove(lines); })
function slove(lines) { // console.log('lines', lines); // ['3', '1', '2', '3'] let target = Number(lines[0]); let arr = []; for(let i = 2; i < lines.length; i++) { arr.push(Number(lines[i])); } let newArr = filter(arr, function(element) { return element !== target; })
for(let i=0; i < newArr.length; i++) { console.log(newArr[i]); } }
// LIOJ 1037:Array filter function filter(arr, callback) { let result = []; for(let i =0; i < arr.length; i++) { if(callback(arr[i])) { result.push(arr[i]) } } return result; }
|