Skip to content

Instantly share code, notes, and snippets.

@having-fun-coding
Created May 11, 2015 19:20
Show Gist options
  • Save having-fun-coding/a401fd1d1ed7f8e97ca7 to your computer and use it in GitHub Desktop.
Save having-fun-coding/a401fd1d1ed7f8e97ca7 to your computer and use it in GitHub Desktop.
Truncate a String | Free Code Camp | Bonfire
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 the three dots at the end add to the string length.
Here are some helpful links:
String.slice()
function truncate(str, num) {
if (str.length > num) {
str = str.slice (0, num-3) + '...';
}
return str;
}
truncate('A-tisket a-tasket A green and yellow basket', 11);
@zerin108
Copy link

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

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

    //console.log(str);
    return str;
}

}

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

@mohamed-avatarinweb
Copy link

function truncate(str, num) {

if(str.length <= num){
return str;
}

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

}

@mushfick
Copy link

function truncate(str, num) {
  if (num <= 3) {
    return str.slice(0, num).concat('...');
  }
  else {
    return str.length > num ? str.slice(0,num).replace(/.{3}$/, '...') : str;
  }
}

@VadimKhismatov
Copy link

function truncate(str, num) {
// Clear out that junk in your trunk
if(num <=3){
return str.slice(0,num) + "...";

}

if (str.length <= num){
return str;

}else if (str.length > num ){

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

}
}

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

@markcodes
Copy link

function truncateString(str, num) {
  // Clear out that junk in your trunk
  var ending = "...";
  if (str.length > num) {
    if (num > 3 ) {
    str = str.slice(0,num-3);
    return str + ending;
    } else {
      str = str.slice(0,num);
      return str + ending;
    }
  } else {
    return str;
  }
}

truncateString("A-", 1);

@lotfymakh
Copy link

function truncateString(str, num) {
// Clear out that junk in your trunk
if(str.length <= num)
{
console.log(str);
return str;
}
else
{
if (str.length < 3 || num < 3 ) {
str = str.slice(0, num) + "...";

}
else
{
str = str.slice(0, num-3) + '...';
}

//console.log(str);
return str;

}

}

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

@mluttrull
Copy link

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

truncateString("A-tisket a-tasket A green ", 3);

@stovla
Copy link

stovla commented Sep 4, 2016

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

} else {
return str;
}

}

@patribezek
Copy link

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

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

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

@Prav92
Copy link

Prav92 commented Sep 17, 2016

function truncateString(str, num) {
// Clear out that junk in your trunk
if(str.length <= num)
{
return str;
}
else
{
if(str.length < 3){
str = str.slice(0, num) + "...";
}
else
{
str = num > 3 ? str.slice(0, num-3) + '...': str.slice(0, num) + '...'; // Clear the last test
}

//console.log(str);
return str;

}
}

@jetyu
Copy link

jetyu commented Oct 19, 2016

Solution 1:Basic(recommend)

function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (str.length > num && num > 3) {
    return str.slice(0, (num - 3)) + '...';
  } else if (str.length > num && num <= 3) {
    return str.slice(0, num) + '...';
  } else {
    return str;
  }
}

Solution 2:Advanced

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

@jamuhuri
Copy link

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

truncateString("A-", 1);

@jonathanpalma
Copy link

jonathanpalma commented May 26, 2017

My solution using Conditional (ternary) Operator:

function truncateString(str, num) {
    return str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
}

@avishai84
Copy link

avishai84 commented Feb 20, 2019

image

function truncateString(str, num) {
const strLength = str.length;
const strOutput = str.substring(0, num);
if(num >= strLength ){
return strOutput+'.';
}else{
return strOutput+'...';
}
}

@p-gold
Copy link

p-gold commented Jun 3, 2019

After a week of dirtying my code editor with this challenge, i later got it right . checkout below
function shortenStr (str, num){
if(str.length > num){ //checks if the length of the given string is greater than the specified number.
return str.slice( 0, num ) + "..." //if the condition above is true, the slice method will start from index [0] of str and stop in the specified number adding "..." as suffix.
} else {return str} // if the condition above is false, then it returns the initial string in the argument.
}
shortenStr("I will always code to improve my skills", 9)

@p-gold
Copy link

p-gold commented Jun 3, 2019

//After a week of dirtying my code editor with this challenge, i later got it right . checkout below function shortenStr (str, num){ if(str.length > num){ //checks if the length of the given string is greater than the specified number. return str.slice( 0, num ) + "..." //if the condition above is true, the slice method will start from index [0] of str and stop in the specified number adding "..." as suffix. } else {return str} // if the condition above is false, then it returns the initial string in the argument. } shortenStr("I will always code to improve my skills", 9)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment