How to keep adding the base values in an array until condition is met?