Created
October 11, 2019 21:39
-
-
Save Said-FD/af8cf736fe9f687c3991a539acac7ecc to your computer and use it in GitHub Desktop.
Example task solution for WIX Junior Frontend Engineer - Kickstart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const solution = (str, num) => { | |
if (num < 1 || num > 500) return 'Characters length is out of the range' | |
const wordsArr = str.split(' ') | |
const wordsLengthArr = wordsArr.map(word => word.length) | |
let resultArr = [] | |
let temp = '' | |
wordsArr.forEach((word, index) => { | |
if (index === 0 && word.length <= num) temp = word | |
else if (temp.length + word.length + 1 <= num) temp = (`${temp} ${word}`).trim() | |
if (temp.length + wordsLengthArr[index + 1] + 1 > num) { | |
resultArr.push(temp) | |
temp = '' | |
} | |
}) | |
resultArr.push(temp.trim()) | |
// console.log(resultArr.length) | |
return resultArr.length | |
} | |
const s = 'SMS messages are really short' | |
const k = 12 | |
solution(s, k) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment