Hi guys, I’m Ceci, I’m trying to solve the data structures and exercises from that course on JavaScript.
This is my implementation.
Please provide some feedback on how to improve the solution or if you have another solution in JavaScript share with me.
class MyArrayClass {
#length;
#data;
constructor(length = 0) {
this.#length = length;
this.#data = [];
}
isFull() {
let newItems = [];
if(this.#data.length === this.#length) {
newItems = [this.#length*2];
}
for(let i = 0; i < this.#length; i++) {
newItems[i] = this.#data[i];
}
return this.#data = newItems;
}
push(item) {
this.isFull();
this.#data[this.#length] = item;
this.#length++;
return this.#data;
}
pop() {
delete this.#data[this.#length - 1];
this.#length--;
return this.#data;
}
validIndex(index) {
if (index < 0 || index >= this.#length) {
throw new Error("Invalid argument excpetion - BANG!!");
}
}
removeAt(index) {
this.validIndex(index);
for (let i = index; i < this.#length; i++) {
this.#data[i] = this.#data[i + 1];
}
delete this.#data[this.#length - 1];
this.#length--;
return this.#data;
}
indexOf(item) {
for(let i = 0; i < this.#length; i++) {
if(this.#data[i] === item) {
return i;
}
}
return -1;
}
max() {
let max = this.#data[0];
for(let i = 0; i < this.#length; i++) {
if(max < this.#data[i])
max = this.#data[i];
}
return max;
}
intersect(other) {
const intersectedArray = this.#data.filter(value => other.includes(value));
return intersectedArray;
}
reverse() {
let newItems = new Array;
for (let i = 0; i < this.#length; i++) {
newItems[i] = this.#data[this.#length - i - 1];
}
this.#data = newItems;
return newItems;
}
insertAt(item, index) {
this.validIndex(index);
for (let i = this.#length; i >= index; i--) {
this.#data[i] = this.#data[i - 1];
}
this.#data[index] = item;
this.#length++;
return this.#data;
}
}
Did you test your implementation against various inputs and edge cases? There are several problems in your code ranging from minor to severe. I only took a glance at it, so there might be more. But here’s what I’ve found.
Initializing an array with length larger than 0 breaks array. Try creating an array of length 1 and push an element to it. See what happens. You need to have a capacity as well as length.
This code doesn’t do what you are trying to do. All it does is create a new array with one element that is length * 2
I wouldn’t call it isFull because the name implies it will return a boolean value without side effects. It is just a conventional thing.
If you decided to use private feature, I would apply it consistently. For example, why is validIndex() and isFull() not private?
In your implementation, how are users supposed to know how many elements are in the array?
max() doesn’t really belong to an array. What happens when array contains non-numeric values?
Also, if max should be there why not min and other aggregating functions? You can see where I’m going.
The message should indicate index out of range.
Usability-wise, pop() should return the last element and not the array. Otherwise, how would the caller access the popped value?
Lastly, there are many places where you return a Javascript array type. If your goal is implementing an array, your array class should return your array.
Did you follow what Mosh asked in this exercise? I’m trying to do that but with JavaScript that’s it, just the methods he asked and what he said they should do. the pop will be the remove in this case the min he did not asked and the error message yeah I could be more explicit but I’m aking help in the implementation of the code, to the people who followed Mosh tutorial, the only thing I find usefull about your reply is that validIndex( ) and isFull( ) should be private.
If you are only seeking for feedbacks from people who took Mosh’s data structure and what’s required in the exercise explicitly. I have nothing more to say.
As of recently, # is the official private keyword in Javascript. Underscore is an old convention used in many dynamic languages. If you must use private fields for some reason in Javascript, # is the way to go because you can actually hide those.
Normally, I wouldn’t bother making something private for the exercise code but I used it anyway in this case.