1-Array_Insert

PHOTO EMBED

Wed Sep 21 2022 03:12:58 GMT+0000 (Coordinated Universal Time)

Saved by @Vrushabh_123

 /*
        Function to insert a new element to the given position using for loop
        @param array, position, element
      */
      const insertFun = (arr, pos, el) => {
        if (pos <= arr.length && typeof pos === "number") {
          for (let i = arr.length - 1; i >= 0; i--) {
            if (i >= pos) {
              data[i + 1] = data[i];
              if (i === pos) {
                data[i] = el;
              }
            }
          }
          return arr;
        } else {
          alert("Out of Range");
        }
      };
content_copyCOPY