Skip to content

Instantly share code, notes, and snippets.

@andanf-e
Created October 29, 2019 17:20
Show Gist options
  • Save andanf-e/2069113e566aa0769f4f22eb23738dcf to your computer and use it in GitHub Desktop.
Save andanf-e/2069113e566aa0769f4f22eb23738dcf to your computer and use it in GitHub Desktop.
const solution = (message, length) => {
const words = message.split(' ');
const filteredMessages = [];
let flag = true;
for (let word of words) {
if (word.length > length) {
flag = false;
break;
}
if (
filteredMessages.length !== 0 &&
filteredMessages[filteredMessages.length - 1].length + word.length <
length
) {
const newMessage = filteredMessages[filteredMessages.length - 1].concat(
' '.concat(word)
);
filteredMessages.splice(filteredMessages.length - 1, 1, newMessage);
} else {
filteredMessages.push(word);
}
}
return flag ? filteredMessages.length : -1;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment