![]() ![]() ![]() If the length of the string is less than or equal to the given number, just return the string without truncating it.With that in mind, we can truncate words by passing in zero as the beginIndex and num as the endIndex "Bastian".slice(0, 4) // returns "Bast" Keep in mind that strings are zero-indexed! So if we wanted to return from the 2-indexed letter of “Bastian” until but not including the 5-indexed letter of “Bastian”, we could do this: "Bastian".slice(2, 5) // returns "sti" slice() where to start the slicing and where to end the slicing. slice() a beginIndex and endIndex, like so. slice() on a string without passing it any additional information, it will return the whole string. slice() extracts a section of a string and returns it as a new string. Concatenating (adding together) strings doesn’t require us to change our data type or structure, so let’s just stick with strings. The only edge case we’re advised about is if the length of str is less than or equal to num, we need to just return str without truncating it.Įxamples/Test Cases: Our provided test cases are pretty straightforward.ĭata Structure: We are given strings and expected to return a string. So for example truncateString('Aluminum', 4) // returns 'Alum.' Ultimately, we want our output to be str truncated - meaning cut off - after num characters with “…” added to the end of the truncated str. It looks like the provided test cases are giving us words, letters, and characters. Understanding the Problem: We have two inputs here: truncateString("Absolutely Longer", 2) should return "Ab.".truncateString("A-", 1) should return "A.".truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2) should return "A-tisket a-tasket A green and yellow basket".truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length) should return "A-tisket a-tasket A green and yellow basket". truncateString("Peter Piper picked a peck of pickled peppers", 11) should return "Peter Piper.".truncateString("A-tisket a-tasket A green and yellow basket", 8) should return "A-tisket.". ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |