This leverage the regular expressions to match three characters at most. I know regular expressions is not the easiest thing to use but in this case it can be quite handy. As it returns an array, joining the items will produce a string back so the signature of the function can be written as follow in a purely functional programming language like PureScript.
formatPhoneNumber :: String -> String -> String
Unfortunately, currying (partially applying parameters) is not built-in in JavaScript. You could implement your own curry function and use it on all your future function definitions. But this would be overkill for this challenge. But if you would ask me how I would implement a currying function in JavaScript, here is my take.
"use strict"functioncurry(callable,...initialParameters){if(typeofcallable!=="function"){thrownewTypeError("Function expected as first argument")}returnfunction(...additionalParameters){constparameters=[...initialParameters,...additionalParameters]if(parameters.length>=callable.length){returncallable(...parameters)}returncurry(callable,...parameters)}}// format :: String -> Number -> String -> Stringconstformat=curry(function(glue,segments,phone){returnphone.match(newRegExp(`.{1,${segments}}`,"g")).join(glue)})// usFormat :: String -> StringconstusFormat=format("-",3)// frenchFormat :: String -> StringconstfrenchFormat=format(".",2)console.log(usFormat("123456789"))// 123-456-789console.log(frenchFormat("1234567890"))// 12.34.56.78.90
Last tip, when creating a function, try to put the data structure at the end, that way you can construct partial application of your function without pain, even in JavaScript. In this case, this means puting the phone number to format at the very end of the parameter's list in your function definition. Hope that helps you in your functional programing journey pal.
What you did is indeed currying but it is not transparent in the sense that if you want to pass all three arguments at once you will have to make three calls. The curry implementation allows you to either pass all arguments at once like a regular function call or partially calling the function. I kinda dislike calling my function multiple times and the only two reasons are aesthetic and the fact that it is less natural to define functions like that but arrow functions syntax makes it a little bit cleaner.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Way to go buddy!
From my perspective, to format the phone number in 3 segments, I would have used this instead of the 3 calls you made.
This leverage the regular expressions to match three characters at most. I know regular expressions is not the easiest thing to use but in this case it can be quite handy. As it returns an array, joining the items will produce a string back so the signature of the function can be written as follow in a purely functional programming language like PureScript.
Unfortunately, currying (partially applying parameters) is not built-in in JavaScript. You could implement your own curry function and use it on all your future function definitions. But this would be overkill for this challenge. But if you would ask me how I would implement a currying function in JavaScript, here is my take.
Last tip, when creating a function, try to put the data structure at the end, that way you can construct partial application of your function without pain, even in JavaScript. In this case, this means puting the phone number to format at the very end of the parameter's list in your function definition. Hope that helps you in your functional programing journey pal.
Hey thanks for the response I will look at regexes next time for this kind of problem. Also I thought currying was supported in ES6?
via
What you did is indeed currying but it is not transparent in the sense that if you want to pass all three arguments at once you will have to make three calls. The curry implementation allows you to either pass all arguments at once like a regular function call or partially calling the function. I kinda dislike calling my function multiple times and the only two reasons are aesthetic and the fact that it is less natural to define functions like that but arrow functions syntax makes it a little bit cleaner.