Javascript, Function that groups strings according to parameter value

Asked

Viewed 48 times

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)

2 answers

0


Better codes will appear, here comes my humble little suggestion:

function test(line_size){
    return function(text){
        if (!text) {
          return [];
        }
        let remainingText = text;
        const result = [];
        while (remainingText.length > 0) {
            const splittedText = (remainingText.length >= line_size) ? remainingText.slice(0,line_size) : remainingText;
            remainingText = remainingText.slice(line_size);
            result.push(splittedText);
        }
        return result;
    }
}


pp = test(8);
result1 = pp('aaa bbb ccc');
result2 = pp('xxxx yy zzzz w');

console.log(result1);
console.log(result2);

  • 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

  • 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.

  • '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

  • 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.

0

I arrived at this code. With the examples you gave, it seems that is ok. You would have to do other tests to see if it meets all the requirements of the exercise...

function mkPretty(line_size) {

  return function(string) {
    const arr = string.replace(/\s{2,}/gm,' ').trim().split('');
    const result = [];
    while (arr.length) {
      result.push(arr.splice(0,line_size || 72).join('').trim());
    }
    return result;
  }

}

let pp = mkPretty(8);

console.log(pp('aaa bbb ccc'));
console.log(pp('aaa      bbb       ccc'));
console.log(pp('xxxx yy zzzz w'));

pp = mkPretty(20);

console.log(pp('dhokgqcoxz ftinckwt cwdyyn rjobtezm zitsjnprri hcdbshpsy ybkzbjtco oyzyihyhboxu kjqqrggm sxtxscr fgdsqqpmotz ohnlzakiw lpyredgaml smpbttbcumj znqrqp rezdtz qojyextfvvne rbcbv qwhtydxgi.'));

  • Thank you very much! It worked perfectly, and this code 'is super compact, but I didn’t quite understand some of the steps, if you don’t mind explaining to me: what is replace doing there? . replace(/ s{2,}/gm,' ')

  • achei um caso que o código parece nao dar a resposta esperada: pp=mkPretty(20)pp("ilfetaoio lgnzievconz ivhsxwk kqaaniqj\neey jsqigfw wcckudxbpov&#xA;scneshx esnzfejxu spbf&#xA;jlnlvhpbmv&#xA;fhkf cqmzboxfs wguyedikkjn. ")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.