# Basic Algorithm Scripting: Truncate a String help

Basic Algorithm Scripting: Truncate a String help
0

I am passing 3/6 tests but can’t seem to figure out why the other three aren’t passing. I’ve tried logging at different stages using the cases which aren’t passing and have so far figured out that it doesn’t work haha.

code:

``````function truncateString(str, num) {
// Clear out that junk in your trunk
let truncate = "...";
let lengthArray = [str.length];
if (lengthArray.length !== num) {
let progress = str.slice(0, num);
let result = progress.concat(truncate);
return result;
}

}

truncateString("A-", 1);
``````

what happens if `lengthArray.length` is equal to `num`?

do you always need to add the dots at the end?

also this array will always have length one… are you sure you want to use that for comparisons?

I’m going to throw in some console.logs to try and help show you what’s going wrong here.

``````function truncateString(str, num) {
// Clear out that junk in your trunk
let truncate = "...";
let lengthArray = [str.length];
console.log(num) // 1
console.log(str.length) // 2
console.log(lengthArray) // [2]
console.log(lengthArray.length) // 1
if (lengthArray.length !== num) { //Here you're incorrectly comparing
// the length of `lengthArray` with `num`. You need the length of `str`.
// `lengthArray` will only ever be 1 because it is a single number.
let progress = str.slice(0, num);
let result = progress.concat(truncate);
return result;
}

}

truncateString("A-", 1);
``````

Hopefully this is enough to help you see what you need to correct. One other thing to check after, though…your if statement checks if two things are not equal, but what happens if they are equal?

I have edited the code based on your suggestions and I am now getting 5/6 tests passing but this one is still failing to pass:

`truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length)` should return “A-tisket a-tasket A green and yellow basket”.

Here is the new code:

``````function truncateString(str, num) {
// Clear out that junk in your trunk
let truncate = "...";

if (str.length > num) {
let progress = str.slice(0, num);
let result = progress.concat(truncate);
return result;
} else if (str.length == num) {
return result.concat(truncate);
} else {
return str;
}

}

``````

what is your code returning in this case?
what do you need to change ?

Code is returning

``````// running tests

``````

``````
should return "A-tisket a-tasket A green and yellow basket". // tests completed ```

console logging the function at the end of the code it gives this:

``````

``````
So, the function is getting truncated by the first if statement because it is using ```.length``` rather than a numerical value to specify when to truncate.  Not sure how to proceed.

Edit: Managed to solve what was wrong, I've changed the second if statement to return an untruncated string.

``````

function truncateString(str, num) {
// Clear out that junk in your trunk
let truncate = “…”;

if (str.length > num) {
let reducedString = str.slice(0, num);
let result = reducedString.concat(truncate);
return result;
} else if (str.length == num) {
** return str**
} else {
return str;

}

}

Solved it, changed the second if statement to return an untruncated string if it is equal to num, as you said in your first post ieathleen, thank you :).

``````function truncateString(str, num) {
// Clear out that junk in your trunk
let truncate = "...";

if (str.length > num) {
let reducedString = str.slice(0, num);
let result = reducedString.concat(truncate);
return result;
} else if (str.length == num) {
return str
} else {
return str;

}

}

``````

now you have the else if and else that do the same thing, can’t you just merge those two together?

anyway, `str.length` gives a number, so a numerical value is still passed there

awesome work with the fixing it on your own!

How would i specify that with -

``````str.length == num || < num
``````

Would that work?

You can just remove the “else if statement” completely. It is not needed.
Although if you did want to merge them, it would be:

``````else if (str.length <= num)
``````

But like I said, you’re better off just writing

``````else
``````

Ah ok, I’m overcomplicating it haha

if you ever want to compare the same value to two different values, never tdo this, it doesnt work
it would be

``````str.length == num || str.length < num
``````

but as @sgedye said, `<=` exist, or as your else if and your else are doing the same thing you can just keep the else
if you have two different statements that do the same thing you can do with only one, in this case the easier way would just be keep the else statement