0
I’m trying to solve the exercise that is in the image, the language and' javascript:
this is the code I created, but it only works for some cases and not for the example given (pp) for example, someone could help me write it more elegantly using the JS tools?
function test(line_size){
return function(text){
let word ="";
let result =[];
let array = text.split(/\s+/);
for (let i = 0; i < array.length; i++) {
if(word.length + array[i].length + 1 <= line_size){
if(word.length === 0){
word += array[i];
}else{
word += " " + array[i];
}
}else{
result.push(word);
word = array[i];
}
}
return result;
}
}
pp = test(8);
pp('aaa bbb ccc')
console.log(pp)
Impressive, I take a long time to come up with a solution to problems, usually start on paper and then try to write on the computer the idea I had, if you don’t mind me asking, how is your process of thinking about the solution until it’s time to write the code? And thank you so much for the correction, I will study what you passed me
– Pirategull
It’s just practice, you do it one way today, you see one person doing it another and you learn it, then another, and you keep updating yourself. Here people with more time of experience I make codes even better, in their vast majority.
– Leonardo Getulio
'Actually, reading your code gave me a lot of new insight. Thank you very much. I was testing with other strings and it doesn’t seem to work for some very small words, for example if I use pp = test(20) result3 = pp("dhokgqcoxz ftinckwt cwdyyn rjobtezm zitsjnprri hcdbshpsy ybkzbjtco oyzyihyhboxu kjqqrggm sxtxscr fgdsqqpmotz ohnlzakiw lpyredgaml smpbttbcumj znqrqp rezdtz qojyextfvvne rbcbv qwhtydxgi. " ) the minor words Dao problem
– Pirategull
I read your exercise more carefully this time and saw that it has some special requirements. You’ll have to implement some rules there to work properly.
– Leonardo Getulio