freeCodeCamp Challenge Guide: Truncate a String

Hello,
My first post here :slight_smile: My solution is
return num >= str.length ? str : num < 3 ? str.slice(0,num)+'...': str.slice(0,num-3)+'...';
I have used ternary operator twice as two if statements. first checks the num>str, then num <3 conditions.

1 Like

I came up with basically the same solution, but the order is different and I used else if:

function truncateString(str, num) {
if (num <= 3) {
return str.slice(0, num) + “…”;
} else if (str.length <= num){
return str;
} return str.slice(0, num - 3) + “…”;
}

This is my solution. It’s basically a shorter version of the basic solution in the guide post.

/*The 1st If statement will check if num is greater than 3 & the length of string is greater than num.
    If so, it'll keep the part of the string from index 0 until index(num-5), then add the dots.*/
  if(num > 3 && str.length > num) {
    return str.slice(0, num-3) + "...";
  }
  //If num is less than or equal to 3, this will slice the string from index 0 to index num, then add the dots.
  else if(num <= 3) {
    return str.slice(0, num) + "...";
  }
  //If the length of the string and the num is the same, it'll return the original string.
  else {
    return str;
  }

I used two if statements

First one checks if num<3

the other one checks the remaining conditions

function truncateString(str, num) {

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

truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length)
1 Like

my solution with ternary operator

function truncateString(str, num) {
  // Clear out that junk in your trunk
  
  return str.length<=num ? str : num<=3 ? str.slice(0,num)+".".repeat(3) :str.slice(0,num-3)+".".repeat(3);
}

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

function truncateString(str, num) {
// Clear out that junk in your trunk

var result=[];
var word=str.split(’’);
if (str.length > num && num > 3)
{
for(i=0;i<num-3;i++)
result[i]=word[i];

    result[num-3]='...';

  return result.join('');
}

else if (str.length > num && num <= 3)
{

  for(i=0;i<num;i++)
    result[i]=word[i];
  
    result[num]='...';

  return result.join('');
}

else
{ return str;
}

}

truncateString(“A-”, 1);

Hello, here is mine
function truncateString(str, num) {
// Clear out that junk in your trunk
var dots ="…";
if(num <= 3){
return str.slice(0,num)+dots;
}
if(num >= str.length){
return str;
}
return str.slice(0,num - dots.length)+dots;
}

My solution below. I wanted to simplify it, but spaced on the idea of using ternary operators inside of the slice method. The advanced solution is often enlightening.

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

Another answer with ternary operator :

function truncateString(str, num) {
  var result = (str.length <= 3 || num <= 3) ? str.slice(0 , num) + "..." :  (str.length > num) ? str.slice(0,num -3)+ "..." :  str ;
  return result;
}
truncateString("Absolutely Longer", 2)
2 Likes

I came up this basic code
function truncateString(str, num) {

if(str.length > 3 && num > 3){
if(str.length <= num){
return str.slice(0,num);
}
return str.slice(0,num - 3) + “…”;
}
return str.slice(0,num) + “…”;
}

truncateString(“A-”, 1);

I am new to programming and want to understand what is the significance of basic , intermediate or advanced level of solution.?
On what basis solution is categorized from basic to advanced?

This is how I truncated the string campers:

function truncateString(str, num) {
// Clear out that junk in your trunk

if(num >= str.length) {

return str;

}else if(num < 3) {

return str.slice(0,num) + '...';

}else{

return str.slice(0,num-3) + '...';

}

return str;
}

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

I challenge, all of you guys, to understand my code:

function truncateString(str, num) {
  // Clear out that junk in your trunk
  valor = "";
  if (num < 5){
    for (i = 0; i < num; i++){
      valor += str[i];
      
    }
  }
  
  else if (num > 5 && num < 35){
    for (i = 0; i < num - 3; i++){
      valor += str[i];
    }
  }
  else if (str.length == num || str.length <= num){
    valor += str;

  }
  
    
  
  if (str.length != num){
    if ((str.length + 2) == num){
      return valor;
    }
    valor += "...";
    return valor;
  }
  else {
    return valor;
  }
}

truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2)

My code, like a couple others, was simpler than the advanced algorithm presented in the answer section above, but it (as far as I can tell) is still a little simpler even than the other one-line answers. Here it is:

function truncateString(str, num) {
    return str.length <= num ? str : str.slice(0, num > 3 ? num - 3 : num) + "...";  
}
truncateString("A-tisket a-tasket A green and yellow basket", 11);

What I did was state that if str.length is smaller than or equal to the num variable, then to return the str. If not, return the str but only between index 0 and either (a) num - 3 (if num is greater than 3) or (b) num, plus the “…”. A bit more concise even than the other one-liners.

My first attempt:

function truncateString(str, num) {
// Clear out that junk in your trunk

if (num >= str.length) {return str;} //for when you don’t need to truncate
if (num > 3) {num = num - 3;} //makes room for the ellipsis in the final string where num > 3
return str.slice(0, num) + ‘…’; // makes the slice simple
}

but after reading about the ternary operator I converted it to the below one liner, but it essentially does the job in exactly the same way.

function truncateString(str, num) {
// Clear out that junk in your trunk
return num >= str.length ? str : str.slice(0, num > 3 ? num -= 3 : num) + ‘…’;
}

how do people put their code examples in a grey box with black font?

1 Like

What ?? What do I wrang please ? I can’t find where is the mistake… it just doesn’t work *****

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

truncateString("Absolutely Longer", 2);

function truncateString(str, num) {
// Clear out that junk in your trunk
var string = ‘’;
if (str.length > num && str.length > 3 && num -3 > 0){
string = str.slice(0,num-3);
string = string + ‘…’;
} else if (str.length > num && str.length > 3 && num > 0){
string = str.slice(0,num);
string = string + ‘…’;
}
else if (str.length > num && str.length < 3){
string = str.slice(0, num);
string = string + ‘…’;
console.log(string);
}

else {
string = str;

}

return string;
}

truncateString(“Absolutely Longer”, 2);

why does this not wok?

function truncateString(str, num) {
var a=’’;
while(str.length>num){
a=str.slice(1,num);
a+="…";}// Clear out that junk in your trunk
console.log(a);
}

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

function truncateString(str, num) {
var theLength=0;
var trunc;
var minus=num-3;
var dots=’…’;

  if(str.length>num && num>3){

//
trunc=str.slice(theLength,minus);

  }else if(num<=3){
    trunc=str.slice(theLength,num);

  }else{
    return str;
  }

// Clear out that junk in your trunk
return trunc+dots;
}

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

Note: It doesnt matter if the code is spaghetti as long as it works I am happy and more motivated.

1 Like