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.
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);
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;
}
}
truncateString("A-tisket a-tasket A green and yellow basket", 8);
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". // tests completed ```
console logging the function at the end of the code it gives this:
“A-tisket a-tasket A green and yellow basket…”
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;
}
}
console.log(truncateString(“A-tisket a-tasket A green and yellow basket”, “A-tisket a-tasket A green and yellow basket”.length));
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;
}
}
console.log(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length));
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