3
\$\begingroup\$

I'm a newbie trying to write my own push() and join(). This isn't very serious, just an exercise.

This is what I got so far

function Errey(...values){
    this.name=values
    }
Errey.prototype={
  constructor:Errey,
    //join method
  myJoin(union = ' '){
  let result=""
  const length = this.name.length
  for(let i=0; i<length; i++)
  { 
   i<length-1 ? result += this.name[i] + union: result+=this.name[i]
  }
  return result
  },
    //push method
    myPush(value){
    this.name = [...this.name, value]
    return this.name.length
    }
  }

//run a few examples
const myArray = new Errey("3","blue","1","brown")
//we'll mutate myArray anyways using const or let
let joined  = myArray.myJoin()
let a = myArray.myPush("hey")
console.log("joined new array: ",joined, "\n\npush-mutated array: ", myArray.name, "\n\npush return value: ", a)

I'm aware an object is returned instead of an array. I guess you might help me out there. Should I use factory functions here? I don't know how would I create a prototype for such a thing...

Any improvements on the main code you'd suggest?

\$\endgroup\$
1
  • \$\begingroup\$ Hello, I added the reinventing-the-wheel tag to your question. \$\endgroup\$ Commented Jul 27, 2020 at 5:51

1 Answer 1

4
\$\begingroup\$

first of all you can use the "new" class syntax instead of changing the prototype directly.
This is how I would do it:

class Errey {
    #currentArray;

    constructor(...elements) {
        this.#currentArray = elements;
    }

    get errey() {
        return this.#currentArray;
    }

    push(...elements) {
        const startLength = this.#currentArray.length;
        elements.forEach((curEl, index) => this.#currentArray[index + startLength] = curEl);
        return this.#currentArray.length;
    }

    join(union = " ") {
        return this.#currentArray.reduce((prev, cur) => prev + union + cur);
    }
}

//run a few examples
const myArray = new Errey("3","blue","1","brown")

console.log(myArray.join(", "));
console.log(myArray.push("Subscribe", "To", "Him"));
console.log(myArray.errey);

push:
the standard array.prototype.push allows for multiple elements to be added at once, so I allow multiple elements and loop over them using forEach, you could also do [...this.#currentArray, ...elements] (or use Array.prototype.concat()).

join:
here I use reduce to create a join operation. Reduce takes the first 2 elements and applies a function on them (in this case a + union + b), makes them the new first element and repeats that until there is only one element left, our joined erray.

I also made the internal Array private using the prefixed # and created a getter (get errey()) instead, this way the internal array can only be changed using the push method.

Using the factory pattern (which I personally prefer):

const Errey = (...elements) => {
    
    const push = (...newElements) => {
        const startLength = elements.length;
        newElements.forEach((curEl, index) => elements[index + startLength] = curEl);
        return elements.length;
    };

    const join = (union = " ") => elements.reduce((prev, cur) => prev + union + cur);

    const get = () => elements;

    return {
        push,
        join, 
        get,
    };
};

const myArray = Errey("3","blue","1","brown")

console.log(myArray.join(", "));
console.log(myArray.push("Subscribe", "To", "Him"));
console.log(myArray.get());
\$\endgroup\$
3
  • \$\begingroup\$ i will take me a couple of days to understand it but will do. thanks! \$\endgroup\$ Commented Jul 27, 2020 at 1:55
  • \$\begingroup\$ @misternobody if you have questions feel free to ask :) \$\endgroup\$
    – Teiem
    Commented Jul 27, 2020 at 2:57
  • \$\begingroup\$ Im getting there ! :-) \$\endgroup\$ Commented Aug 2, 2020 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.