Truncate-a-string algorithm challenge wording

On this challenge, there is some wording that I don’t quite understand based on one of the expected results. Part 1 of the challenge is clear. no confusion on this part.

Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a … ending.

Note that inserting the three dots to the end will add to the string length

Part 2 of the challenge is this:

However, if the given maximum string length num is less than or equal to 3, then the addition of the three dots does not add to the string length in determining the truncated string.

But look at this expected result.
truncateString(“A-”, 1) should return “A…”.

Does this result fall into part 2 of the challenge definition? If it does, why are we
adding to the string length?

Sorry, I am just confused by the challenge description and looking at the expected outcomes.

The wording is not extremely clear, but looking at the expected outcomes might indeed help.

truncateString(“A-tisket a-tasket A green and yellow basket”, 11)
should return “A-tisket…”

Num is set to 11, therefore greater than 3, and the return string accounts for the three dots in the string length.

"A-tisket...".length is indeed 11

Looking instead at the other example

truncateString(“A-”, 1)
should return “A…”.

Num is set to 1, and the return string does not account for the three dots.

"A…".length is indeed 4, while "A".length is the requested 1.

Basically, it is the second argument num and its length that matters, not the length of the string itself. The three dots are added either way, but they configure in the computation of the string length only if num is greater than 3.

Hope it is of some help.


Thank you. I think I will now be able to write the algorithm with your help.

Glad to be of some assistance :grinning:

Spoiler Alert: This code works and Function Description is worded differently from Algorithm specifications.

** Function: Truncates a String. 1st argument is a string, 2nd argument is string's 
**  maxLength. 
** If string's size is larger than 2nd argument: maxLength, the string will be truncated
**   If maxLength > 3 truncate string at maxLength less 3 characters and add '...' to 
**     string's end.
**   Else truncate string at maxLength and add '...' to string's end.
** Otherwise String does not require truncation.
** @function truncateString
** @summary  Takes a string and a Multiplier to concatenate itself. Return "" if Multiplier < 
**     1
** @param 	{string} str
** @param   {number} multiplier
** @returns {string} string concatenation
function truncateString(str, maxLength) {
	'use strict';
	var result = "";
  var strSize = str.length;
  if (strSize > maxLength) {
    if (maxLength > 3) {
      result = str.slice(0, maxLength - 3) + "...";
    } else {
      result = str.slice(0, maxLength) + "...";
  else {
    result = str;
  return result;

FYI - You can surround your code with [spoiler] and [/spoiler] tags to blur out a solution.

thank you, i will edit with those appropriate tags.

So this is how I solved the problem. The last condition gave me an issue for some reason, but this solution solved the given problem. Other solutions might be more streamlined, yet this works.

function truncateString(str, num) {
  if (str.length <= num){
    return str;
  else if (str.length <= 3){
    return str.slice(0, num) + "...";
  else if (str.length >= num){
    if (num > 3){
    return str.slice(0, num-3) + "...";
    else if (str.length >= num){
      return str.slice(0, num) + "...";
  return str;

truncateString("A-tisket a-tasket A green and yellow basket", 11);

@jzaneg - Please use spoiler tags when posting working solutions.

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

In general, when responding to another camper’s topic, try to give hints to help the person solve their issue. In this particular case, the OP already came up with a working solution with the help given to him.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.

Thanks for your understanding.

working one line solution :slight_smile:

function truncateString(str, num) {
 return num < str.length ? str.substr(0, num) + "..." : str;
1 Like