//title: Back Single Jersey Sheet (javascript with knitout module)
// Builds a sheet of single jersey (all knits) fabric on the back bed. (There isn't much of a point to doing this, but you can.)

//include knitout module:
const knitout = require('knitout'); //include knitout module
const K = new knitout.Writer({ carriers:['1', '2', '3', '4', '5', '6'] });
K.addHeader('Machine', 'Kniterate');

//Parameters:
let min = 1; //needle number of left edge
let max = 20; //needle number of right edge
let rows = 20; //number of rows to knit
let carrier = "3"; //carrier name

// ---- set up initial loops and yarn carrier ----
//Bring in carrier:
K.in(carrier);

//On Kniterate machines, carriers start on the left,
//so will start tucking onto needles left-to-right,
//and will be sure to tuck the leftmost needle in the first pass:
for (let n = min; n <= max; n += 1) {
	//tuck alternating needles, making sure to do the left edge:
	if ((n - min) % 2 === 0) {
		K.tuck("+", 'f' + n, carrier);
	}
}
//now, moving right-to-left, tuck the needles that were not tucked on the first pass:
for (let n = max; n >= min; n -= 1) {
	if ((n - min) % 2 !== 0) {
		K.tuck("-", 'f' + n, carrier);
	}
}

//knit three plain rows to allow cast-on stitches to relax:
// three isn't set in stone here -- it's just convenient
// for this example code to have the carrier end up on the right.
for (let n = min; n <= max; n += 1) {
	K.knit("+", 'f' + n, carrier);
}
for (let n = max; n >= min; n -= 1) {
	K.knit("-", 'f' + n, carrier);
}
for (let n = min; n <= max; n += 1) {
	K.knit("+", 'f' + n, carrier);
}

//move loops to the back bed:
for (let n = min; n <= max; n += 1) {
	K.xfer('f' + n, 'b' + n);
}
// ---- single jersey sheet (back bed) ----
//Create a single-jersey (all knits) sheet by alternating left- and right-going rows of back knits:
for (let r = 0; r <= rows; r += 1) {
	if (r % 2 === 0) {
		//left-going row:
		for (let n = max; n >= min; n -= 1) {
			K.knit("-", 'b' + n, carrier);
		}
	} else {
		//right-going row:
		for (let n = min; n <= max; n += 1) {
			K.knit("+", 'b' + n, carrier);
		}
	}
}

// ---- take carrier out and drop remaining loops ----
//Send carrier back to its parking location:
K.out(carrier);

//drop loops:
for (let n = min; n <= max; n += 1) {
	K.drop('b' + n);
}

K.write('back-single-jersey-sheet.kniterate.k');
