Jamgreen
Jamgreen

Reputation: 11039

Define variable inside while loop

How is

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

rewritten so I call .peek() every time the loop runs, but only define it once?

I have thought about

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

but since I modify stack with stack.pop() inside the while loop, I guess the value of stack.peek() is changing every time, so I guess I have to redefine the variable inside the loop, but

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

also seems a bit wrong, so should it be something like

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

or

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

Upvotes: 1

Views: 300

Answers (3)

guest271314
guest271314

Reputation: 1

How is

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

rewritten so I call .peek() every time the loop runs, but only define it once?

Sorry for the consuion. I have created Array.prototype.peek = () => this[this.length - 1]. Is it bad practice to 'mess' with Array.prototype?

Note,

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

returned 3 , here, using function keyword .

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

using arrow function

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

returned undefined


You could alternatively use expression stack.length -1 as condition within while loop; e.g.;

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}`);

Upvotes: 1

user663031
user663031

Reputation:

Consider using while (true) with a break:

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

In theory you could also do:

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

but that's pretty ugly. It's roughly equivalent to write

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

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

A for loop is not a bad idea either, and could be written as:

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

which again avoids duplicating the call to stack.peek().

Upvotes: 1

Anthony Rutledge
Anthony Rutledge

Reputation: 7564

At minimum, this should work.

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.
}

Define a variable before the loop condition, then update the variable as the last statement inside the loop. I'm sure you would have arrived at that solution eventually. :-)

Upvotes: 0

Related Questions