fork download
  1. Array.prototype.find2 = function find2(fn) {
  2. for(const x of this) {
  3. if (fn(x)) {
  4. return x
  5. }
  6. }
  7. }
  8.  
  9. Array.prototype.filter2 = function filter2(fn) {
  10. const result = []
  11. for(const x of this) {
  12. if (fn(x)) {
  13. result.push(x)
  14. }
  15. }
  16. return result
  17. }
  18.  
  19.  
  20. const array = [1,2,3,4]
  21. const result1 = array.find2(x => x > 2)
  22. console.log(`find: ${result1}`)
  23.  
  24. const result2 = array.filter(x => x > 2)
  25. console.log(`filtered: ${result2}`)
  26.  
Success #stdin #stdout 0.04s 16596KB
stdin
Standard input is empty
stdout
find: 3
filtered: 3,4