Truncate a String --Need Help--

Truncate a String --Need Help--
0

#1

Tell us what’s happening:
Hi guys! Can you help me? I also tried this codes from the hint button:

  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;
  }

}

and:

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

but… The console keeps saying:

// running test
truncateString("A-tisket a-tasket A green and yellow basket", 8) should return "A-tisket...".
truncateString("Peter Piper picked a peck of pickled peppers", 11) should return "Peter Piper...".
// tests completed

please help mee

Your code so far


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;
  }

}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

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


#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

A good first step is to look at what your code is actually returning for the test cases. This can be done in FCC using the browser’s console, but I’ve put your code in a repl.it to show you.


#4

Why is it always returning undefined hahahha, I Really don’t understand :disappointed_relieved::sob:


#5

The undefined at the end just has to do with the repl session finishing. I actually call your function in the two console statements. That’s where you can see what it’s returning.


#6

How! I’m totally confused right now, I’m really sorry


#7

I really can’t solve it, OMG, can you please help me?


#8

Instead of “A-tisket…”, your code returns “A-tis…”
Instead of “Peter Piper…”, your code returns “Peter Pi…”

This tells you why the tests are failing. From this you can start to see a pattern: both of these are 3 characters shorter than they should be. That should give you a starting point to look at your solution and see if you can figure out where the error in your solution might be.


#9

I really don’t get it sirr :sob:


#10

Spend some time with it. I tried to give you a nudge toward what your problem is, but it will probably take more than 2 minutes to figure it out. Go tinker with your code specifically looking for what logic results in some of your results being too short. You probably want to use console.log statements to walk through your code and see if you can figure out when the error occurs.


#11

When I looked at the solution I was also confused, they didn’t give me any further details that allowed me to understand that I was supposed to do what they were doing in the solutions . Why does my solution pass the tests despite being really basic but using the solutions provided by them the tests don’t pass?

I had already did this exercise before the update to the curriculum but I don’t really recall whether it was like this before or whether not but I think they changed the exercise and made it more basic.


#12

I also tried this code:

function truncateString(str, num) {
  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;
  }
}

but the same thing happens. Please Help Me Guyss!!


#13

The number 3 isn’t necessary to pass the challenge


#14

What do you mean sir?


#15

All you have done is copy and paste code from some source you think is the correct code. Can you explain line by line what the code you wrote does?

How do I know you copied and pasted this code from somewhere else? Because it is the first example in the Guide when you click on Get a hint. Fortunately or unfortunately for you, the challenge has changed since the Guide was written, so that code no longer works as is.

Instead of copying/pasting code, try writing out (plain language) the steps you would take to solve the same challenge by hand on paper. Once you have written the steps (your algorithm), then try converting your written steps to JavaScript code. If you get stuck, then post your written code and we can help guide you to a solution.


#16

Ok sir, I’ll do what you said.


#17

Sir, I got the answer, thanks for encouragement :grinning:

function truncateString(str, num) {
  if (str.length < num) return str;
  var truncStr = str.slice(0, num);
  var truncStrArr = truncStr.split(' ');
  var truncStrArrLen=truncStrArr.length;
  
  if(truncStrArrLen > 1 &&
    truncStrArr[truncStrArrLen - 1] !== str.split(' ')[truncStrArrLen - 1]) {
    truncStrArr.pop();
    truncStr = truncStrArr.join(' ');
  }
  return str.length > num ? truncStr + '...' : truncStr;
}

console.log(truncateString("A-tisket a-tasket A green and yellow basket", 

#18

@JanzenGo can you break down your answer for clarification?


#19

What part(s) do you not understand?


#20

Within the 2nd ‘if’ statement after the && regarding truncStrArr[truncStrArrLen - 1] … and so forth