sande
August 18, 2021, 7:05am
1
function truncateString(str, num) {
let string = ""
if(num){
for (let i=0; i<num; i++){
string += str[i]
}
return string+"...";
}else {
return string `// this is not working`
}
}
console.log(truncateString("A-tisket a-tasket A green and yellow basket", 10));
A-tisket a… //output
Challenge: Truncate a String
Link to the challenge:
Jagaya
August 18, 2021, 7:24am
2
The function will always be called with num, so it will never enter the else-code.
Also try calling it with a number bigger than the length of the string and you will notice the first part also isn’t working.
ILM
August 18, 2021, 9:49am
4
sande:
if(num){
if num what?
when is it that you need to truncate the string?
sande
August 18, 2021, 11:32am
5
if the string length is greater than num.
ILM
August 18, 2021, 2:59pm
6
then you need to use that as condition for the if statement
sande
August 18, 2021, 3:18pm
7
ok but the dots are not going
sande
August 21, 2021, 4:32am
9
it is working now
if(num<str.length){
return str.slice(0,num)+"..."
}else{
return str
}
2 Likes
system
Closed
February 19, 2022, 7:14pm
11
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.