【问题标题】:Define variable inside while loop在while循环中定义变量
【发布时间】:2016-09-10 04:57:08
【问题描述】:

怎么样

while (
  stack.peek() in ops &&
  p(stack.peek()) >= 10
) {
  str += stack.pop();
}

重写所以每次循环运行时我都会调用.peek(),但只定义一次?

我想过

const peek = stack.peek();
while (
  peek in ops &&
  p(peek) >= 10
) {
  str += stack.pop();
}

但由于我在while循环内用stack.pop()修改stack,我猜stack.peek()的值每次都在变化,所以我想我必须重新定义循环内的变量,但是

let peek = stack.peek();
while (
  peek in ops &&
  p(peek) >= 10
) {
  str += stack.pop();
  peek = stack.peek();
}

好像也有点不对,应该是这样的

while (
  let peek = stack.peek() &&
  peek in ops &&
  p(peek) >= 10
) {
  str += stack.pop();
}

for (
  let peek = stack.peek();
  peek in ops && p(peek) >= 10;
  peek = stack.peek()
) {
  str += stack.pop();
}

【问题讨论】:

  • 嗯,倒数第二个例子在语法上是不正确的,所以改用for 循环?我不清楚问题是什么。使用任何有效的代码?
  • @Jamgreen。我试了一下。让我知道你的想法。
  • @Jamgreen “但只定义一次”是什么意思? ?
  • 我很困惑。 stack 是否包含函数?那么stack.peek() 返回一个函数?那么peek 是一个函数吗?那样的话,peek in ops怎么会有意义?
  • “对不起,我已经创建了Array.prototype.peek = () => this[this.length - 1]。” 注意,var stack = [1,2,3]; stack.peek() 在这里返回了3,这里使用了function 关键字。 Array.prototype.peek = function() { return this[this.length - 1]}var stack = [1,2,3]; Array.prototype.peek = () => this[this.length - 1]; stack.peek() 使用箭头函数返回 undefined

标签: javascript while-loop


【解决方案1】:

考虑将while (true)break 一起使用:

while (true) {
  const peek = stack.peek();
  if (!(peek in ops) || p(peek) < 10) break;
  str += stack.pop();
}

理论上你也可以这样做:

while (
  (peek => peek in ops && p(peek) >= 10)(stack.peek())
) {
  str += stack.pop();
}

但这很丑。大致相当于写

function pop(stack) {
  const peek = stack.peek();
  return peek in ops && p(peek) >= 10;
}

while(pop(stack)) str += stack.pop();

for 循环也不错,可以写成:

for (let peek; peek = stack.peek(), peek in ops && p(peek) >= 10; ) {
  str += stack.pop();
}

这再次避免重复调用stack.peek()

【讨论】:

  • 我确实采用了while (true) 方法:D 我将其更改为while (stack.length &gt; 1) { const peek = stack.peek(); if (peek in ops &amp;&amp; p(peek) &gt;= 10) { str += stack.pop(); } else { break; } }
  • @这似乎过于复杂。解决方案非常非常简单。
  • @AnthonyRutledge 提供的解决方案似乎没有比原来的问题更复杂?
  • @guest271314 这是用复杂性代替简单性的奇怪理由。
  • @AnthonyRutledge 实际上,这并不重要。然而,当我解释 OP 的问题时,它是关于处理在代码中两次调用 stack.peek() 的不令人满意的性质。
【解决方案2】:

怎么样

while (
  stack.peek() in ops &&
  p(stack.peek()) >= 10
) {
  str += stack.pop();
}

重写所以每次循环运行时我都会调用 .peek() ,但只定义 一次?

对不起。我创建了 Array.prototype.peek = () => 这[this.length - 1]。 “弄乱” Array.prototype 是不好的做法吗?

注意,

var stack = [1,2,3]; 
stack.peek();

返回 3 ,在这里,使用 function 关键字。

Array.prototype.peek = function() { return this[this.length - 1]}

使用箭头函数

Array.prototype.peek = () => this[this.length - 1]; stack.peek()

返回undefined


您也可以在while 循环中使用表达式stack.length -1 作为条件;例如;

var stack = [-2, -1, 0, 1, 2, 3, 4, 5],
  n = 0;

while (stack.length - 1 && (peek = stack.pop())) {
  // do stuff
  n += (curr = peek * 10) >= 10 ? curr : n;
  delete curr;
  console.log(`peek:${peek}`);
}

console.log(`n:${n}, peek:${peek}`);

【讨论】:

    【解决方案3】:

    至少,这应该可以工作。

    var peek = stack.peek();  //Declaration (once) and initialization
    
    while ((peek in ops) && (p(peek) >= 10))
    {
      str += stack.pop();
      peek = stack.peek()    //Re-assignment after modifying stack.
    }
    

    在循环条件之前定义一个变量,然后将该变量更新为循环内的最后一条语句。我相信你最终会找到那个解决方案。 :-)

    【讨论】:

    • 这个答案是最好的答案,放下手。
    猜你喜欢
    • 2015-10-09
    • 2016-01-12
    • 2015-05-01
    • 2016-02-19
    • 2018-03-14
    • 1970-01-01
    • 2022-07-07
    • 2013-10-29
    • 1970-01-01
    相关资源
    最近更新 更多