Truncate a String....help

Truncate a String....help
0

#1

Tell us what’s happening:
why this code is not working?

Your code so far


function truncateString(str, num) {
  // Clear out that junk in your trunk.
  let s = str.substr(0, num+1) + "...";
  return s;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.23 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string/


#2

Hi,

I did it, You know how I did it I don’t have good knowledge about Problem, JavaScript. Tip: You need to understand what they want to solve, every point you have to note in mind when you got some point to solve then try to understand another point which was reaming So, Again Start practice and thinking About the problem which is in below in image.

Here is the solution but you need to solve all the problem by self, Don’t get some code from other You need to have your own Potention.

function truncateString(str, num) {
  // Clear out that junk in your trunk
  var str;
  if( str.length > num )
  {
    return str.substr(0, num) + '...';
  }
  else 
  {
    return str;
  }


}

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

Solve this problem Again and Again and try to solve more from other site then you will be involved.

Be patience!


#3

Hi there,

A tip would be to print or write to console.log() to see what is going wrong.
I did this with your code and I found that it is not removing the last space, so in the first error for example, the return if you write to console is "A-tisket " + “…” which gives A-tisket …
The exercise says that it should return A-tisket… , so you have to check for ending white spaces and remove them.
Hope this helped! :slight_smile:


#4

thank you so much for helping :slightly_smiling_face:


#5

thank you for the tip, it was really helpful :blush:


#6

No worries.
I maybe should have said that you should take that tip with you on all exercises and in everything you code, console.log() (write out text, depends on the programming language) at all places where it is possible, then you will always see if functions or commands ore doing what you expect them to do. In this way you have a bit more control or overview over the code.
Happy coding :slight_smile: