Hi there and thanks for your article, I also like to train my data algorithm in JavaScript. I tend to try and implement them using only functions and recursion if needed. Here is my attempt I wanted to share with you.
const createStack = () => { const state = { popped: null, data: [], }; const stack = { push: item => (state.data = [item, ...state.data], state.head = item, state.size = state.size + 1, stack), pop: () => (state.popped = state.data[0] ?? null, state.data = state.data.slice(1), state.popped), peek: () => state.data[0] ?? null, size: () => state.data.length }; return stack; }; const language = createStack(); language .push("Typescript") .push("Angular") .push("JS") .push("C++"); console.log(language); // [object Object] console.log(language.pop()); // C++ console.log(language); // [object Object] console.log(language.pop()); // JS console.log(language); // [object Object]
Not the easiest to read given I used the comma operator pretty much for every method but it gets the job done using some one-liners.
Good 👍👍
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi there and thanks for your article, I also like to train my data algorithm in JavaScript. I tend to try and implement them using only functions and recursion if needed. Here is my attempt I wanted to share with you.
Not the easiest to read given I used the comma operator pretty much for every method but it gets the job done using some one-liners.
Good 👍👍