-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
27 lines (25 loc) · 735 Bytes
/
script.js
File metadata and controls
27 lines (25 loc) · 735 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
26
27
function messageSplit (k,s) {
const arrWords = k.split(' ');
const valid = k.length <= 500 && k.length > 0
&& arrWords.every(arrWord => arrWord.length <= s)
&& Number.isInteger(s)
&& typeof s == 'number'
if(!valid) {
return -1
}
let fullMsg = [];
let cutMsg = [];
for (let i = 0; i <= arrWords.length; i++){
if (i === arrWords.length) {
fullMsg.push(cutMsg.join())
} else if (cutMsg.join().length + arrWords[i].length <= s) {
cutMsg.push(arrWords[i])
} else {
fullMsg.push(cutMsg.join(' '))
cutMsg = []
i--
}
}
return fullMsg
}
console.log(messageSplit('SMS messages are really short', 12))