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

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 answers

573 users

How to split an array and add the first part to end in Node.js

1 Answer

0 votes
class CArray {
	print(arr) {
	    const size = arr.length;
	    
		for (let i = 0; i < size; i++) {
			process.stdout.write(arr[i] + " ");
		}

		process.stdout.write("\n");
	}
	reverse(arr, start, end) {
		for (let i = start, j = end; i <= end && j > i; i++, j--) {
			let temp = arr[i];
			arr[i] = arr[j];
			arr[j] = temp;
		}
	}
	split(arr, split_point) {
	    const size = arr.length;
	    
		if (size <= 1 && split_point < 1 && split_point >= size) {
			return;
		}

		// reverse first part
		this.reverse(arr, 0, split_point - 1);
		
		// reverse second part
		this.reverse(arr, split_point, size - 1);
		
		// reverse all array 
		this.reverse(arr, 0, size - 1);
	}
}


const obj = new CArray();
	
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];

let split_point = 3;
	
obj.split(arr, split_point);

obj.print(arr);	
	
	
	
	
/*
run:
       
4 5 6 7 8 9 0 1 2 3 
       
*/	

 



answered Nov 29, 2021 by avibootz

Related questions

1 answer 276 views
1 answer 268 views
1 answer 283 views
1 answer 263 views
1 answer 182 views
...