1

Write a function removeColumns that accepts two arguments:

originalGrid (two-dimensional array)

numColums (number)

removeColumns should return a new grid with the correct number of columns removed. If numColumns = 1, remove one column. If numColumns = 2, remove two columns.

removeColumns([[1, 2, 3],
               [1, 2, 3],
               [1, 2, 3],
               [1, 2, 3]], 2);
/* => [[1],
       [1],
       [1],
       [1]]
*/

I tried:

function removeColumns (originalGrid, numColumns) {

  let newGrid = [];

  //if numColumns = 1, remove 1 column

  for (let col = 0; col < originalGrid.length; col ++)

    if (numColumns === 1) {
      newGrid.pop(col)
    }

  //if numColums = 2, remove 2 columns

    else if (numColumns === 2) {
      newGrid.pop(col + 1)
    }

  return newGrid;

}

And there are several issues with this.

I understand how to remove the last item from one array:

function removeItem(array) {

  let poppedArray = array.pop();

  return array;

}

But I do not understand how to loop through three consecutive arrays in a grid, and remove the last item from all three.

[1, 2, 3]
[1, 2, 3]
[1, 2, 3]

I think once I understand the concept of how to remove one column, I will understand how to remove two columns.

EDIT: Link to codepen

2 Answers 2

1

Here's an example I made that will remove one column of your choosing. I'm not sure which of the 3 columns you want to remove when you say "remove 2 columns" but I think this should get you there. I think what you are missing is the splice method.

function removeColumns(arrayOfArrays, column) {
  for (var i = 0; i < arrayOfArrays.length; i++) {
    arrayOfArrays[i].splice(column - 1, 1);
  }
 return arrayOfArrays;
}

var arrayOfArrays = [
  [1, 2, 3],
  [1, 2, 3],
  [1, 2, 3],
  [1, 2, 3]
];
var results = removeColumns(arrayOfArrays, 1);

console.log(results);

jsFiddle here: https://jsfiddle.net/a5rqLbuh/

If I was doing this for raw speed I'd incorporate a library such as async to send all of the arrays off at the same time to be modified and then brought back together. I do not think this will preserve the original order of the array of arrays, but it's something to look into.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry but this isn't returning anything. It needs to return a new array or arrayOfArrays. Please see the link to the codepen to test this code: codepen.io/FullstackAcademy/pen/yKOgVR?editors=0010
Then just return a new array...I've edited my answer
0

A workaround:

function removeColumns(originalGrid, numColumns) {
  for (let i = 0; i < originalGrid.length; i++) {
    let row = originalGrid[i];
    row.reverse();
    row.splice(0, numColumns);
    row.reverse();
  }
  return originalGrid;
}

This way, you can remove "numColumns" number of elements from the start of the array, because .splice() always removes elements from left to right (even if you start splicing at the back of an array).

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.