reduce function working