Truncate a string need help plz

Tell us what’s happening:
I don’t want to look at to solution, I need someone who can give me a hint. I want to truncate these string, if the length of the string is more than num.

I wrote a ‘solution’ and it didn’t work, I don’t know why. I creat a new array (let arr = [ ];).

I check if the length of the string is more than num (if (str.length > num) ).

Then I creat a variable result equals to the result of the array that slice method will give ( let result = str.slice(0, num);).

I use the spread method to copy the result and input it in my empty array that I created at the begining (arr = […result];).

the instruction tells me that the truncated string should have (…) ending. I push it(…) in my arr array(return arr.push(’…’);). else I return str .

What’s the problem with this solution ?

  **Your code so far**

function truncateString(str, num) {
let arr = [];
if (str.length > num) {
let result = str.slice(0, num);
arr = [...result];
return arr.push('...');
}
return str;
}

truncateString("A-tisket a-tasket A green and yellow basket", 8);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36.

Challenge: Truncate a String

Link to the challenge:

What do you see if you run this?

function truncateString(str, num) {
  let arr = [];
  if (str.length > num) {
    let result = str.slice(0, num);
    arr = [...result];
    console.log(arr);
    return arr.push('...');
  }
  return str;
}

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

should return the string

A-tisket...

.

truncateString("Peter Piper picked a peck of pickled peppers", 11)

should return the string

Peter Piper...

.

truncateString("A-", 1)

should return the string

A...

.

truncateString("Absolutely Longer", 2)

should return the string

Ab...

.

What do you see in the console when you run the modified code I provided? I added a console.log statement.

You function is returning the return value of Arr.Push() . (Push method adds an item to array and return the length of the new array)

1 Like

the console.log returns a array of letter :
[ ‘A’, ‘-’, ‘t’, ‘i’, ‘s’, ‘k’, ‘e’, ‘t’ ]

I can’t explain this but all I know is that my code is not correct

This is an important clue. You have an array. It looks like you took apart the string and put in into an array. The function output needs to be all joined back up again. Is there some JavaScript function that can help you do that?

2 Likes

I know what you mean. But I am a little bit confused. My code is almost correct and I have to add something ? I know the join method (.join) could do something like that.

What happened when you tried the join method?

The console returns nothing.

This returns nothing?

 console.log(arr.join(''))
2 Likes

You should return the join method and make sure the console statement is before the return statement.

Otherwise, nothing will display to the console.

Why do you need an array?

  1. .push() returns the length of the array after pushing. It does not return the array.
const names = ['John', 'Jill', 'Jack'];
console.log(names.push('Bob')); // 4
console.log(names.length); // 4
  1. You are supposed to return a string not an array.
3 Likes

Thanks ! Yes it returns A-ticket. And then I try to return arr.push(’…’). Because I want this => A-ticket…

Why not do push, and then return the join method?

1 Like

Yes, but even if the console return A-ticket… I can’t pass the challenge.

let arr = [  ];

if (str.length > num) {

  let result = str.slice(0, num);

  arr = [...result];

  arr.push('...');

  return console.log(arr.join(''));

}

I’ll try to be clearer.

It should be this

 arr.push('...');
 console.log(arr.join(''))
 return arr.join('') 

That is what I meant by put the console.log before the return statement.

make sense?

1 Like

I’m still curious why you think you need an array?

You literally have everything you need inside result except the “…” which you can just concatenate to the end of the string.


You can obviously code this however you’d like and getting this to work with an array first is totally fine. In fact, you should.

3 Likes

Thank you ! It works finally

1 Like

Hooray!

I would also follow @lasjorg 's advice and see if you can solve it without going through the trouble of creating an array.

It is a much simpler solution.

But I can relate to you.

When I went through the algorithm sections I had a lot of complicated solutions too that could have been much simpler.

But it is all part of the learning process. :grinning:

2 Likes