Home > Software engineering >  How can I check an array of arrays to see if an internal array has both values greater than the valu
How can I check an array of arrays to see if an internal array has both values greater than the valu

Time:08-29

I have come to this code, but it results in an error. The idea is to have, at the end of the program, an array of arrays with all the original arrays that meet the condition of having both values greater than the values of the previous array.

var data = [[68, 150], [70, 155], [55, 160]];
var result = [];

for(i=0; i<data.length; i  ){
    
    var firstPosterior = data[i 1][0];
    var lastPosterior = data[i 1][1];
    var firstAnterior = data[i][0];
    var lastAnterior = data[i][1];
    
    if(
        firstPosterior > firstAnterior &&
        lastPosterior > lastAnterior
        ) {
        result.push(firstPosterior, firstPosterior);
    }
}

CodePudding user response:

First you have a compilation error at

for(i=0; i<data.length; i  ) // i is not defined. You should use let, var to declare it

Here it is an example


var data = [[68, 150], [70, 155], [55, 160]];
var result = [];

for(let i=0; i<data.length -1; i  ){
    
    var firstPosterior = data[i 1][0];
    var lastPosterior = data[i 1][1];
    var firstAnterior = data[i][0];
    var lastAnterior = data[i][1];
    
    if(
        firstPosterior > firstAnterior &&
        lastPosterior > lastAnterior
        ) {
        result.push(data[i 1]);
    }
}

I am adding in the result only arrays that met the condition.

  • Related