DEV Community

loading...

Discussion on: Daily Challenge #9 - What's Your Number?

Collapse
celyes profile image
Ilyes Chouia

Better late than never ;p

Here's my attempt in PHP

function formPhoneNumber(array $integers): string
{

    if(count($integers) != 10) {
        throw new Exception('Please provide a valid array!');
    }

    $phone = "(";
    foreach($integers as $index => $number){
       if($number < 10 && $number >= 0){
            $phone .= $number;
            if($index == 2){
                $phone .= ") ";
            }elseif($index == 5){
                $phone .= "-";
            }
       }else{
            throw new Exception("One of the numbers is invalid!");
       }
    }
    return $phone;

}
echo formPhoneNumber([4, 5, 5, 4, 5, 1, 7, 8, 9, 0]);