DEV Community

Cover image for Make immutable pop(), push(), shift(), unshift()?
Kurapati Mahesh
Kurapati Mahesh

Posted on • Edited on

Make immutable pop(), push(), shift(), unshift()?

We know very well how these functions work in Javascript. So, here I am going to write immutable implementations for the same functions as per my understanding. Please do comment your suggestions/concerns/questions. I will always happy to update this article.

function pop(arr) {
    let newArr = [...arr];
    newArr.length = arr.length - 1;
    return newArr;
}

function push(arr, ...item) {
    const newArr = [...arr];
    if (item.length >= 1) {
        for (let i = 0; i < item.length; i++) {
            newArr[newArr.length] = item[i];
        }
    }
    return newArr;
}

function shift(arr) {
    let [a, ...b] = arr;
    return b;
}

function unshift(arr, ...item) {
    const newArr = [];
    if (item.length >= 1) {
        for (let i = 0; i < item.length; i++) {
            newArr[i] = item[i];
        }
    }
    newArr.push(...arr);
    return newArr;
}

Enter fullscreen mode Exit fullscreen mode

Thank you! Happy Reading!


💎 Love to see your response

  1. Like - You reached here means. I think, I deserve a like.
  2. Comment - We can learn together.
  3. Share - Makes others also find this resource useful.
  4. Subscribe / Follow - to stay up to date with my daily articles.
  5. Encourage me - You can buy me a Coffee

Let's discuss further.

  1. Just DM @urstrulyvishwak
  2. Or mention
    @urstrulyvishwak

For further updates:

Follow @urstrulyvishwak

Top comments (3)

Collapse
 
tracygjg profile image
Tracy Gilmore • Edited

Hi Vishwak,
Your implementation of shift has a critical flaw. The variables a and b should have been declared as local variables instead of adding to the global scope. Otherwise the function was a good cue for how the other functions can be written more concisely as follows.

    function pop(arr) {
        return arr.slice(0, Math.max(0, arr.length - 2));
    }

    function push(arr, ...items) {
        return [...arr, ...items];
    }

    function shift(arr) {
        return arr.slice(1, Math.max(0, arr.length));
    }

    function unshift(arr, ...items) {
        return [...items, ...arr];
    }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
urstrulyvishwak profile image
Kurapati Mahesh • Edited

You have written in declarative style.

Also, your pop can't work as expected. And, does Math.max required?

I followed little imperative.

Thanks. I will update for shift.

Collapse
 
tracygjg profile image
Tracy Gilmore

Vishwak, The Math.max guards against the deduction of length going negative but you are correct there was an error. These are better.

    function pop(arr) {
        return arr.slice(0, Math.max(0, arr.length - 1));
    }

    function push(arr, ...items) {
        return [...arr, ...items];
    }

    function shift(arr) {
        return arr.slice(1, arr.length);
    }

    function unshift(arr, ...items) {
        return [...items, ...arr];
    }
Enter fullscreen mode Exit fullscreen mode