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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,938 questions

51,875 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
...