DEV Community

loading...

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

Collapse
margo1993 profile image
margo1993
package utils

import (
    "errors"
    "fmt"
    "strconv"
)

func FormatPhoneNumber(phoneNumberArray []int) (string, error) {

    if len(phoneNumberArray) != 10 {
        return "", errors.New("Array length must be 10")
    }

    return fmt.Sprintf("(%s) %s-%s", intArrayToString(phoneNumberArray[:3]), intArrayToString(phoneNumberArray[3:6]), intArrayToString(phoneNumberArray[6:])), nil
}

func intArrayToString(intArray []int) string {
    number := ""

    for _, num := range intArray {
        number += strconv.Itoa(num)
    }

    return number
}