Create a Queue Class

Failing the following 3 testcases but I can’t tell why…?

The dequeue method should remove and return the front element of the queue

The front method should return value of the front element of the queue

The size method should return the length of the queue

Your code so far


function Queue () { 
    var collection = [];
    this.print = function() {
        console.log(collection);
    };
    // Only change code below this line
    this.enqueue = function(newItem){
        collection.concat(newItem);
    };
    this.dequeue = function(){
        return collection.shift();
    };
    this.front = function(){
        return collection[0];
    };
    this.size = function(){
        let sizeQueue = collection.length;
        return sizeQueue;
    };
    this.isEmpty = function(){
        if(collection.length !== 0){
            return true;
        }else {
            return false;
        }
    };
    // Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-queue-class

I think you should use push instead of concat.

Thanks @ppiron. But my code was not failing the enqueue testcase so why would I change that…?

In any case, I tried your fix and now front and isEmpty are failing…? I’m starting to think that there is a bug in the test scripts…?

The logic in the isEmpty method should be reversed. Right now it returns true when the length is not zero.

1 Like