如何退出循环?

huangapple go评论67阅读模式
英文:

How can I drop out of the loop?

问题

我尝试编写一个可以将数组向左旋转的函数,例如1,2,3,4,5,6,7,8,9,旋转后变为4,5,6,7,8,9,1,2,3。一开始我使用了以下这个函数,但结果是4,2,3,7,5,6,1,8,9。因此,我认为它没有退出循环,因为它在循环中只执行了一次。请问有人可以帮助我吗?非常感谢!

var a = [1,2,3,4,5,6,7,8,9];
var len = a.length;
for (i = 0; i < 3; i++) {
    var b = a[i];
    var j = i;
    for (k = 0; k < a.length; k++) {
        j += 3;
        if (j < a.length) {
            a[i] = a[j];
            i = j;
        } else {
            j -= 3;
            a[j] = b;
            break;
        }
    }
}

console.log(a);

希望这可以帮助你。

英文:

I try to write a function which can rotate the array to the left,eg 1,2,3,4,5,6,7,8,9
and turn to 4,5,6,7,8,9,1,2,3. At first place, I use this funciton below ,but the result is 4,2,3,7,5,6,1,8,9.
Therefore, I think it does not drop out of the loop because it only executes once in the loop. Please anyone could help me with this? Any help would be appreciated! Thanks in advance.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var a =[1,2,3,4,5,6,7,8,9];
var len =a.length;
for (i=0;i&lt;3;i++ )
{   
    var b = a[i];
    var j = i;
    for(k=0;k&lt;a.length;k++)
      {
        j+=3;
        if(j&lt;a.length)
        {
            a[i] = a[j];
            i=j;
        }
        else {
            j-=3;
            a[j]=b;
            break;
        }

    }   
}

console.log(a);

<!-- end snippet -->

答案1

得分: 1

我不确定你正在使用的方法,但使用 shiftpush 可以轻松实现这个目标:

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var len = a.length;
for (i = 0; i < 3; i++) {
  a.push(a.shift());
}

console.log(a);

输出:

4,5,6,7,8,9,1,2,3

shift 从数组中移除第一个元素。这里有一些关于它的文档

push 将一个元素添加到数组的末尾。这里有一些关于它的文档

英文:

I'm not sure of the approach you are using, but using shift and push will achieve this very easily:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var a =[1,2,3,4,5,6,7,8,9];
var len = a.length;
for (i = 0; i &lt; 3; i++) {
  a.push(a.shift());
}

console.log(a);

<!-- end snippet -->

Output:

4,5,6,7,8,9,1,2,3

shift removes the first item from an array. Here are some docs for it.

push pushes an item onto the end of an array. Here are some docs for it.

答案2

得分: 0

如果您想要不使用数组方法的算法,可以像这样实现:

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(a);

// 旋转迭代(这里向左旋转3次),可以根据需要旋转任意次数。
for (let i = 0; i < 3; i++) {
  // 存储数组的“第一个成员”和“最后一个索引”
  let last = a.length - 1;
  let first = a[0];

  // 循环存储从第1个到倒数第2个元素(不包括最后一个元素)
  for (let j = 0; j < last; j++) {
    a[j] = a[j + 1];
  }

  // 最后将第一个元素添加到最后一个索引
  a[last] = first;
}

console.log(a);
英文:

If you want algorithm with no array methods you can do it like this :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var a=[1,2,3,4,5,6,7,8,9];
console.log(a);

//Iteration of rotation (Here rotated left 3 times) this can be as many as you want.
for(let i=0;i&lt;3;i++){
  //Store &#39;First Member&#39; and &#39;Last Index&#39; of array
  let last = a.length - 1;
  let first = a[0];

  //Loop to store every (n+1)th term to nth except the last one
  for(let j=0;j&lt;last;j++){
    a[j] = a[j+1];
  }

  //Finally add first item to last index
  a[last] = first; 
}

console.log(a);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月6日 23:05:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定