Taken from Execute Program
Use reduce to write a function that behaves like filter. You can do this with only one reduce and no other loops.
filter([1, 2, 3], num => num >= 0) Expected: [1, 2, 3]
filter([1, 2, 3], num => num > 1) Expected: [2, 3]
filter([1, 2, 3], num => num > 5) Expected: []
filter([null, undefined], num => true) Expected: [null, undefined]
filter([], num => true) Expected: []
This is the answer I came up with:
Solution tested in REPL:
https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABMGAbKBTATgCgIZZYBciAgoXgJ4A8eYlAfADSIR6qoBGeEA1iXUoBKRAG8AsAChEiVBiiIsGAM4h0Jcliq16DRAF5EAbQC6UmQSwA6JQBMQEDDhwB9FhBCEMYKCP16cNg5uPkDPJR8hEQB+RRU1KCsABxBlAAswr0jEElMWUyFzOKhPJCVVdCkAXykpCARlODkrVDgAcxwigHougAEoZQBaGDawOCUilHRsHCMARhYAJhYAZhMWMBAAWwM9TZ29OcLJER7jZcQ1qSA