DEV Community

Discussion on: Daily Challenge #17 - Double Trouble

Collapse
 
p810 profile image
Payton Bice • Edited

PHP:

function cola (int $x, array $y): string {
    $z = count($y);

    while ($x > $z) {
        $x = round($x / $z);
    }

    return $y[$x - 1];
}
Collapse
 
choroba profile image
E. Choroba

It returns b for cola(10, array("a", "b", "c", "d")) which I think should return c.

Collapse
 
p810 profile image
Payton Bice

Thank you, that's a good catch - I fixed this by replacing floor() with round().

Thread Thread
 
choroba profile image
E. Choroba

I'm still not sure it's correct. cola(9, array(1, 2)) returns 2 instead of 1.

Thread Thread
 
p810 profile image
Payton Bice

Hm, you're right. It looks like I didn't test it thoroughly enough yesterday, and the tests I did came back with the right answers for the wrong reason. I'm trying to get better at solving math problems so I'll look more into this when I'm off work.