ThousandEyes Interview Question

Implement fold in javascript.