Skip to content

Instantly share code, notes, and snippets.

@Said-FD
Created October 11, 2019 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Said-FD/af8cf736fe9f687c3991a539acac7ecc to your computer and use it in GitHub Desktop.
Save Said-FD/af8cf736fe9f687c3991a539acac7ecc to your computer and use it in GitHub Desktop.
Example task solution for WIX Junior Frontend Engineer - Kickstart
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