Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用for循环和If/Else在队列中循环_Javascript_Loops_Queue - Fatal编程技术网

Javascript 使用for循环和If/Else在队列中循环

Javascript 使用for循环和If/Else在队列中循环,javascript,loops,queue,Javascript,Loops,Queue,嗨,谢谢你花时间看这个 我正在尝试实现以下输出: Iteration 0: Queue 1 = 2345678910 | Queue 2 = Iteration 1: Queue 1 = 3579 | Queue 2 = 2 Iteration 2: Queue 1 = 57 | Queue 2 = 23 Iteration 3: Queue 1 = 7 | Queue 2 = 235 Iteration 4: Queue 1 =

嗨,谢谢你花时间看这个

我正在尝试实现以下输出:

Iteration 0:
Queue 1 = 2345678910 | Queue 2 =
Iteration 1:
Queue 1 = 3579       | Queue 2 = 2
Iteration 2:
Queue 1 = 57         | Queue 2 = 23
Iteration 3:
Queue 1 = 7          | Queue 2 = 235
Iteration 4:
Queue 1 =            | Queue 2 = 2357
这应该是“
我不会撒谎,这显然是学校作业!(虽然截止日期已经过去了,我不想轻易地得到分数。我真的想知道为什么我不能让它生效)
我确实进行了深入的搜索,但我发现的所有类似的东西都是使用数组实现的。(教授不允许这样做)

是一把显示我所处位置的小提琴:

这似乎是我的问题,它不会删除最后一项

if (this.first == this.last) {
  this.first = null;
  return this.first.content;
  this.length--;
}    
我用各种各样的控制语句把它弄得乱七八糟,以找到逻辑问题

当我开始的时候,我很确定我可以做到如下:

循环队列1并将当前值保存到(x) --在队列1中循环,将第一个项目移动到队列2 ----如果/Else当前项可被x整除,则移动到Else重新排队

我把它换成了while循环,但是最后一个项目被移除了,它被卡住了

// find the primes function
function fp() {
  fillQueue(); // call the fillQueue function below

  document.getElementById("output").innerHTML += "Ctrl " + "|it. " + it + " |queue length " + q1.length + " |x " + x + " |current x" + cx + " |Q1: = " + q1.toString() + " | Q2: = " + q2.toString() + " | Q3: = " + q3.toString() + "<br />";

  while (q1.length >= 0) {
    dq1();
    cx = x;
    q3.enqueue(cx);

    while (q1.length >= 0) {
      dq1();
      eval();
      it++;
    }

    while (q2.length >= 0) {
      dq2();
      d1.enqueue(x);
    }
  }
//查找素数函数
函数fp(){
fillQueue();//调用下面的fillQueue函数
document.getElementById(“输出”).innerHTML++=“Ctrl”+“| it.”+it+“|队列长度”+q1.length++“| x”+x+“|当前x”+cx+“| q1:=”+q1.toString()+“| Q2:=”+Q2.toString()+“| Q3:=“+Q3.toString()+”
”; 而(q1.length>=0){ dq1(); cx=x; q3.排队(cx); 而(q1.length>=0){ dq1(); eval(); it++; } 而(q2.length>=0){ dq2(); d1.排队(x); } }
我建议使用不同的方法和循环,当第一个数字元素小于该数字时,直到进行检查。输出协议,将第一个数字元素推到倍数,然后过滤
数字,检查索引零处的数字倍数

最后进行另一个协议输出

函数计算(){
var number=document.getElementById('number')。值,
numbers=Array.apply(null,{length:number-1}).map(函数(u,i){返回i+2;}),
倍数=[];
while(数字[0]<数字){
document.getElementById('out').innerHTML+=numbers.join('''+'| | |')+multiples.join(''+'\n';
倍数推送(数字[0]);
数字=数字。过滤器(函数(a){
返回%1个数字[0];
});				
}
document.getElementById('out').innerHTML+=numbers.join('''+'| | |')+multiples.join(''+'\n';
}
计算

记住:
=
是分配,
=
(或者更好,
=
)用于比较。这行代码会给你带来一些麻烦。
it=n-1
。这也应该是
谢谢你的回复…修复了迭代问题,让我更接近了…仍然有一些问题需要解决,因为输出仍然不正确谢谢你这么多…我喜欢…教授不允许使用数组。我将尝试使用队列实现此功能。我的解队列过程中出现问题…它不会删除最后一项-(