Home > Software design >  JavaScript array returns deepEqual
JavaScript array returns deepEqual

Time:04-13

I want to return an array

function estanParaTrasplantar(alturasDePlantas) {
  let plantasMayoresA20Cm = [];
  for (let planta of alturasDePlantas) {
    if (planta.altura > 20) {
      agregar(plantasMayoresA20Cm, planta);
    }
  }
  return plantasMayoresA20Cm;
}

When I run [21, 29, 5, 20] I get this output

[] deepEqual [ 21, 29 ]

How can I return the array without the deepEqual stuff? Thanks

CodePudding user response:

Instead of using agregar, filter out numbers greater than 20

function estanParaTrasplantar(alturasDePlantas) {
  return alturasDePlantas.filter(x => x > 20)
}

Note

for of will pick some weird array prototype values hence the weird things you're getting.

CodePudding user response:

I edited Jorge's code and it worked. Thanks for all the help!

function estanParaTrasplantar(alturasDePlantas) {
  let plantas = alturasDePlantas;
  function maximo(lista){
    let mayores= [];
    lista.map(function(num){if (parseInt(num)>20){mayores.push(parseInt(num));}});
    return mayores;
  }
  return(maximo(plantas));
}

CodePudding user response:

No se puede saber mucho sin conocer tu función "agregar"(probablemente aqui este tu error), para agregar cada planta.

can't know much without knowing your "agregar" function (probably your mistake here), to add each plant.

let plantas = [21, 29, 5, 20];
function maximo(lista){
    let mayores= [];
    lista.map(function(num){if (parseInt(num)>20){mayores.push(parseInt(num));}});
    return mayores;
}
let buena = maximo(plantas);
console.log(buena);

este funciona bien(si le quieres hechar un ojo), manten tu codigo simple, usa push y funciones ya definidas en las API para minimizar tus errores

this one works fine(if you want to take a look), keep your code simple, use push and functions already defined in APIs to minimize your errors

  • Related