Unclear Instructions for writing 'Queues' in Data Structures

Unclear Instructions for writing 'Queues' in Data Structures
0.0 0


Hey there, I was working through the new data structures, and ran into a few problems. When writing my Queue, I wrote it in the manner the example was given, writting my methods using the this.method way, and console.log() the results instead of returning them.
Later, while testing my results, I ran into trouble. I think it mostly has to do with the binding of the this keyword, since used this way it would be referring to the global object. I considered rewritting Queue as an object instead, and maybe use the .bind() function. I wasn’t sure if this would work the same way for functions. How cant I work around this?


A link to the challenge and your code, please?


Yea sure! The link is here: https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-queue-class/

My code for it is here:

function Queue () { 
    collection = [];
    this.print = function() {
    // Only change code below this line
    this.enqueue = function(elem) {
        collection = [...collection, elem]

    this.dequeue = function() {
        let firstElem = collection[0];
        let tmpArr = [];
        let arrNewLen = collection.length;
        for(i=1; i < tmpArr; i++){
        collection = tmpArr;

    this.front = function(){

    this.size = function() {

    this.isEmpty = function() {
        if(collection.length == 0){
    // Only change code above this line



Hey there, so I rewrote the Queue as a ES2015 class instead, and binded the methods. I tested it and it works, which makes me think that the original function version probably had problems with the binding of the ‘this’ keyword.

My code for the class is as follows, if anyone’s curious:

class myQueue1 {
		this.collection = arr
		this.print = this.print.bind(this);
		this.enqueue = this.enqueue.bind(this);
		this.dequeue = this.dequeue.bind(this);
		this.front = this.front.bind(this);
		this.size = this.size.bind(this);
		this.isEmpty = this.isEmpty.bind(this);
	print() {
		this.collection = [...this.collection, elem];
		let firstElem = [];
        let tmpArr = [];
        let arrNewLen = this.collection.length;
		for(let i=1; i < arrNewLen; i++){
        this.collection = tmpArr;
		return this.collection[0];
		return this.collection.length;
		if(this.collection.length == 0){
             return true;
            return false;


You need to read your error messages.

When I run your original code, I get:

collection is not defined

I take that to mean that collection is not defined. In your code:

collection = [];

It is initialized but not defined. After I fix that, I run into a similar problem with your looping variable i.

When I fix those, it starts working someone. The tests that fail?

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

But that function isn’t returning anything. There are similar problems with front and size.

When I fix those problems, it passes. The instructions were clear, you just didn’t follow them closely enough.


Hey Kevin, I was wondering what you did to fix the collection? What did you populate it with if the original Queue function takes no input args?


I’m not at my computer, but it has an enqueue function, yes? That can add things.


If “fixing” the collection was a separate question, I put let in front of it, but var would also work.


I tried help but there are several errors in ur code.
If done correctly U dont need a class, a function constructor is perfectly fine.
But seems u allready made the challenge?
good luck


Hey there Kevin, one of the reason I didn’t add a ‘let’, ‘var’, or ‘this.’ keyword before the ‘collection’ was because that’s part of the code that the instructions said not to change. If you go to the link of the challenge, it specifies to only change code within the two commented lines. I feel like they either should’ve added a keyword before the ‘collection’ variable already, or let us create the variable.


That is an excellent point. In the morning I’ll see if the issue has been raised in the github.