Building a compose function with reduce: const compose = (.fns) => (value) => fns.reduceRight((acc, fn) => fn(acc), value) Īnd now using it to compose splitByTilde and first functions. So we can compose those functions to build our final getName function. If indexStart is equal to inde圎nd, substring () returns an empty string. In particular: If inde圎nd is omitted, substring () extracts characters to the end of the string. This method modifies the original array and returns the removed elements as a new array. It lets you change the content of your array by removing or replacing existing elements with new ones. The algorithm is: split by the colon and then get the first element of the given list. Description substring () extracts characters from indexStart up to but not including inde圎nd. The splice () method is a built-in method for JavaScript Array objects. Let's build a first function: const first = (list) => list To get the first element we can use the list operator. Example: splitByTilde("john smith~123 Street~Apt 4~New York~NY~12345") // So now we can use our specialized splitByTilde function. We want to make this "john smith~123 Street~Apt 4~New York~NY~12345" into this const split = (separator) => (text) => text.split(separator) So the first thing would be the split function. For example, str.slice (1, 4) extracts the second character through the fourth character (characters indexed 1, 2, and 3 ). slice () extracts up to but not including inde圎nd. Changes to the text in one string do not affect the other string. This string.split("~") gets things done.Īnother functional approach using curry and function composition. Description slice () extracts the text from one string and returns a new string.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |