DEV Community

loading...

Discussion on: Daily Challenge #260 - Subtract the Sum

Collapse
jurerotar profile image
Jure Rotar • Edited

Simple PHP solution using array_reduce

function substractTheSum(int $number): string {
    $fruitList = [
        1 => 'kiwi',
        2 => 'pear',
        // ...
    ];
    while($number > 100) {
        $number -= array_reduce(str_split((string) $number), fn($carry, string $num) => $carry += (int) $num);
    }
    return $fruitList[$number];
}