A
A
artemNeal2022-02-16 17:41:56
JavaScript
artemNeal, 2022-02-16 17:41:56

How to convert the elements of a column in a two-dimensional array, for example, to '0' or remove if the sum of the column is less than zero?

Help, please, solve the problem. In a two-dimensional array, remove columns where the sum of the column elements < 0.
The array must be 5 by 5, filled with random numbers.
I found the sums, but I don’t understand how to delete a column or change the elements in it (((

let A = new Array(n);

for (let row=0; row<A.length; row++){
    A[row] = new Array(m)
    for (let col=0; col<A[row].length; col++) {
        A[row][col] = Math.floor(Math.random()*10) -10;
        console.log(A[row][col]);

        document.write(A[row][col] + ' ');
}

document.write('<br>');

}

//Сумма колонок 

SumCol =[];

for (let col=0; col<A.length; col++){
    Sum = 0;
    for (let row=0; row<A[col].length; row++) {
        Sum += A[row][col];
}

if (Sum < 0) {
    for (let i=0; i<A.length; i++) {
        A[row][col] ='*';
}

console.log(A);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2022-02-16
@Rsa97

The simplest is to store the array transposed, that is, not [row][col], but [col][row]. Then the removal is reduced to a single splice() .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question