如何使用可变数量的项创建数组,然后增加其中一些项

时间:2013-01-08 04:50:12

标签: javascript arrays

我正在构建一个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;
});

1 个答案:

答案 0 :(得分:0)

这不是问题的答案,因为它目前正在制定(akonsu已经在他的评论中回答了问题的标题),也没有直接谈论这个问题。

然而,既然我明白她想要完成什么(并且她对其他方法持开放态度),我认为这本质上是一个非常好的问题,
所以我发布这个是为了帮助其他人了解她真正希望用她发布的代码完成什么,并帮助她更详细地定义算法变化和最终结果/输出可能/应该是什么。

她希望在 y 数字(前一行所在位置的针迹)上尽可能均匀地均匀分布 x 数字(每行额外针迹数)。

img http://i48.tinypic.com/1fxp21.gif

她目前的基本公式为:y/(x+1)
包括舍入会导致:

----------      10 stitches
-----+-----     add 1, every 5
---+----+---    add 2, every 3.3333
---+--+---+--   add 3, every 2.5
--+--+--+--+--  add 4, every 2
--+-+--+--+-+-- add 5, every 1.6667

工作example (in jsFiddle)可能如下:
Javascript

function handler(){
  var i, d=document,                                    //saving some code
      base=Number(d.getElementById('base').value),      //get row input
       add=Number(d.getElementById('add').value) + 1,   //get stitches to add
       ret='',          //return var
       tmp=0,           //temp var
       crr=0,           //carry var
       per=base/add;    //calculate period

  for (i = 1; i < add; i++) {   //start at 1 instead of 0
    tmp=Math.round(i * per);    //round first period
    ret+=tmp-crr+' + ';         //first period with previous period
    crr=tmp;                    //carry period
  }
  ret+=Math.round(add * per)-crr;          //last normal stitches
  d.getElementById('out').innerHTML=ret;   //output result
}

HTML:

Stitches on row: <input type="text" 
                          id="base" 
                       value="10"
                    onchange="handler();"
                 >
<br>
Stitches to add: <input type="text" 
                          id="add" 
                       value="1"
                    onchange="handler();"
                 >
<br>
<button onclick="handler();">calculate</button>
<br>
  Every + is a extra stitch: <br>
<div id="out">&nbsp;</div>

我认为这会像她目前所期望的那样奏效。 但是,我认为可以找到一种模式,我不知道如何以一种简单的方式做到这一点。的任何吗

但我认为这只适用于从左到右的东西,而不是像毛衣袖子那样的圆圈。然后她可能需要一些看起来更像“旋转木马”的东西:

'轮播'风格(公式:((y+x)/x)-1):

----------      10 stitches
----------+     add 1, every 10
-----+-----+    add 2, every 5
---+----+---+   add 3, every 3.3333
--+---+--+---+  add 4, every 3.5
--+--+--+--+--+ add 5, every 2

正如您所看到的,分布是不同的。

祝你的项目好运!