Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
Why does my JS queue only return first word instead of whole string?

Why is it only dequeuing 'first' instead of 'first in line'? As you can see in the following test result the expected value is "first in line", but the received value is only "first", what is going on?

this is my queue class:

function Queue() {}

Queue.prototype.arr = [];
Queue.prototype.sz = 0;

Queue.prototype.enqueue = function(n) {
  this.sz++;
  this.arr.push(n);
}

Queue.prototype.dequeue = function() {
  if(this.sz === 0) {
  return undefined;
  }
  else if(this.sz > 0){
  this.sz--;
  return this.arr.shift();
  }
}

Queue.prototype.size = function() {
  return this.sz;
}

this is my test:

it('returns queued string', function() {
    queue.enqueue('first in line');
    expect(queue.size()).toBe(1);
    expect(queue.dequeue()).toBe('first in line');
  });

and this is the test result:

● returns queued string

    expect(received).toBe(expected) // Object.is equality

    Expected: "first in line"
    Received: "first"

      76 |     queue.enqueue('first in line');
      77 |     expect(queue.size()).toBe(1);
    > 78 |     expect(queue.dequeue()).toBe('first in line');
         |                             ^
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ok here's how I solved it, I rewrote it like this:

class Queue {
  constructor() {
  this.arr = [];
  this.sz = 0;
 }

enqueue(n) {
  this.sz++;
  this.arr.push(n);
}

dequeue() {
  if(this.sz === 0) {
  return undefined;
  }
  else if(this.sz > 0){
  this.sz--;
  return this.arr.shift();
  }
}

size() {
  return this.sz;
}
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!