-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay28.js
More file actions
24 lines (19 loc) · 984 Bytes
/
Day28.js
File metadata and controls
24 lines (19 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//* Coding Challenge: Truncate a String
//*-
//* Write a function called truncateString that takes two parameters:
//? str: A string that needs to be truncated.
//? maxLength: An integer representing the maximum length of the string allowed.
//? The function should truncate the input string str if its length exceeds the specified maxLength. If truncation occurs, the function should add '...' to the end of the truncated string.
const truncateString = (str,count) =>{
if(count <=0) {
return str;
}
if(str.length > count) return str.slice(0,count).concat("...");
}
// Example usage:
console.log(truncateString("A-tisket a-tasket A green and yellow basket", 8));
// Output: "A-tisket..."
//* Constraints:
//? The input string str will contain only alphanumeric characters and punctuation marks.
//? The maximum length maxLength will be a positive integer.
//? The output string length should not exceed maxLength + 3 characters due to the addition of '...'.