我正在构建一个 javascript 小部件,以帮助针织者在遇到“在一行中均匀增加 X 针数”的图案指令时确定在增加之间放置的针数。问题描述为here。
到目前为止,只要结果是偶数,我的代码就可以工作。
如果数字不均匀,我一直试图弄清楚如何使它工作。我认为我需要做的是用针迹组数填充一个数组,然后在数组中递增,向每个组添加 1,直到没有剩余。
但我不知道如何 a) 用可变数量的项目填充数组 - 例如:有多少组针迹?我需要数组中的项目数。b) 将每组的针数存储在数组中的每一项中 c) 将数组中的每一项加 1,直到剩余为 0。
我也对其他方法持开放态度。
这是我到目前为止的代码。我还没有添加 if/else 条件,也没有添加 for 循环。任何想法或想法将不胜感激。提前致谢!
$('#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;
});