Unclear Instructions for writing 'Queues' in Data Structures

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

#1

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?


#2

A link to the challenge and your code, please?


#3

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() {
        console.log(collection);
    };
    // 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++){
            tmpArr.push(collection[i]);
        }
        collection = tmpArr;
        console.log(firstElem);
    }

    this.front = function(){
        console.log(collection[0]);
    }

    this.size = function() {
        console.log(collection.length);
    }

    this.isEmpty = function() {
        if(collection.length == 0){
             console.log(true);
        }
        else{
            console.log(false);
        }
    }
    // Only change code above this line
}

Queue.isEmpty;

#4

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 {
	constructor(arr){
		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() {
		console.log(this.collection);
	}
	
	enqueue(elem){
		this.collection = [...this.collection, elem];
	}
	
	dequeue(){
		let firstElem = [];
		firstElem.push(this.collection[0]);
		
        let tmpArr = [];
        let arrNewLen = this.collection.length;
        
		for(let i=1; i < arrNewLen; i++){
            tmpArr.push(this.collection[i]);
        
		}
        this.collection = tmpArr;
        return(firstElem);
	}
	
	front(){
		return this.collection[0];
	}
	
	size(){
		return this.collection.length;
	}
	
	isEmpty(){
		if(this.collection.length == 0){
             return true;
        }
        else{
            return false;
        }
	}
}

#5

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.


#6

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?


#7

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


#8

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


#9

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


#10

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.


#11

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