


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("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.".Īnd some other - substr() - which provide range of substring and 'length' can be positive only: string.substr(start,length)Īlso some maintainers suggest that last method string.substr(start,length) do not works or work with error for MSIE. so: string.substring(from) positive or negative. String.substring(from,to) as plus if 'to' index nulled returns the rest of string. Need to say due to last discussion in comments, that jQuery is very much more often renewable extension of JS than his own parent most known ECMAScript. However, your method will works out of jQuery function, inside simple Javascript. In other words answer Erickson suggest really perfect solution. So, you can't use slice() inside jQuery because slice() is jQuery method for operations with DOM elements, not substrings. Sorry for my graphomany but post was tagged 'jquery' earlier.
