I’m trying to rewrite the if else structure from the 2nd block of code as a switch statement in the first block of code… but I’m missing something. A few of the test cases are still failing.
function truncateString(str, num) {
switch ((str, num)) {
case str.length > num:
return str;
case num <= 3:
str = str.slice(0, num) + "...";
default:
str = str.slice(0, num - 3) + "...";
}
return str;
}
truncateString(“Absolutely Longer”, 2);
returns “Absolutely Longe…”
should be returning “Ab…”.
function truncateString(str, num) {
if (str.length <= num) {
return str;
} else if (num <= 3){
return str.slice(0, num) + '...';
} else {
return str.slice(0, num - 3) + '...';
}
}
function truncateString(str, num) {
switch ((str, num)) {
case str.length <= num:
str = str;
break;
case num <= 3:
str = str.slice(0, num) + "...";
break;
default:
str = str.slice(0, num - 3) + "...";
break;
}
return str;
}
I don’t understand why the first case of str.length is less than or equal to num is ignored… they are both 43
but instead it is still falling through to the default.
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”.