I'm building a javascript widget to help knitters determine the number of stitches to place between increases when faced with a pattern instruction that reads "Increase X number of stitches evenly across a row." The problem is described here.
So far my code works as long as the result is an even number.
I am stuck trying to figure out how to make it work if the number isn't even. I think what I need to do is populate an array with the number of groups of stitches, then increment through the array, adding 1 to each group until there is no leftover.
But I can't figure out how to a) Populate an array with a variable number of items--e.g.: How many groups of stitches are there? I need that number of items in the array. b) Store the number of stitches in each group in each item in the array c) Add 1 to each item in the array until the leftover is 0.
I'm open to other approaches, too.
Here's the code I have thus far. I haven't added the if/else conditional yet, nor have I added the for loop. Any thoughts or ideas will be very much appreciated. Thanks in advance!
$('#calculateIncrease').submit(function() {
//get the number of stitches in the row
var stitchesInRow = $('input#stitchesInRow').val();
//get the number of stitches to increase by
var increaseByHowMany = $('input#increaseByHowMany').val();
//add one to this number
var divideBy = parseInt(increaseByHowMany,10) + 1;
//divide the number of stitches in row by increase plus one
var stitchesBetween = parseInt(stitchesInRow,10)/divideBy;
var leftover = stitchesInRow % divideBy;
//if there is no leftover
//return the result
$('#howManyStitches').html("Put " + stitchesBetween + " stitches between each increase.");
//if there is a leftover
var stitchesPerGroup = Math.floor(stitchesBetween);
//take divideBy, which is also the number of groups of stitches
//create an array that contains the number of groups (divideBy)
var stitchesArray = new Array(divideBy);
//each array item populated by the integer that is the first half of the division
//for each array item, add 1 until leftover = 0
//there should be some groups that get one added and others that do not
return false;
});