Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,106 questions

40,778 answers

573 users

How to print a string in N equal parts with Node.js

1 Answer

0 votes
function PrintParts(str, parts) {
    const length = str.length;
    
    if (length % parts != 0) {
        console.log("No equal parts");
        return;
    }
    
    const part_size = parseInt(length / parts);
    
    let s = "";
    for (let i = 0; i < length; i++) {
        if (i % part_size == 0 && i != 0) {
            console.log(s);
            s = "";
        }
        s += str.charAt(i); 
    }
    console.log(s);
}

const str = "node.js c++ c java python c#";
const parts = 4;

PrintParts(str, parts);




/*
run:

node.js
 c++ c 
java py
thon c#

*/

 





answered Oct 4, 2022 by avibootz
...