-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmessage2.js
More file actions
25 lines (21 loc) · 985 Bytes
/
Copy pathmessage2.js
File metadata and controls
25 lines (21 loc) · 985 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
25
function solution(message, k) {
if (k < 3) return "...";
if (message.length <= k) return -1;
let words = message.split(" ");
let truncatedMessage = "";
for (let word of words) {
// Ensure we consider spaces correctly when appending words
if (truncatedMessage.length + word.length + (truncatedMessage ? 1 : 0) + 3 > k) {
break;
}
truncatedMessage += (truncatedMessage.length === 0 ? "" : " ") + word;
}
return truncatedMessage.length === 0 ? "..." : truncatedMessage + " ...";
}
// Test cases
console.log(solution("And now here is my secret", 15)); // "And now here ..."
console.log(solution("There is an animal with four legs", 15)); // "There is an ..."
console.log(solution("super dog", 4)); // "..."
console.log(solution("how are you", 20)); // how are you
console.log(solution("A quick brown fox jumps over the lazy dog", 10)); // "A quick ..."
console.log(solution("Truncate this message", 5)); // "..."