Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
How to prevent execution when someone Is absent?

I'm currently working on a slack bot. The purpose is that a developer will be selected for an update everyday at 8 am. Currently I'm trying to figure out how to prevent an execution when someone is absent.

var result_arr = [];
function setValue (value) {
  result_arr = Object.values(JSON.parse(JSON.stringify(value)));
  return result_arr;
  
}
db.connect(function(err) {
    db.query("SELECT * FROM developers", (err, result, fields) => {
        if (err) {
        console.log(err);
        } else {
        // selected = all.map(item => item.selected);
        setValue(result);
        removeMax();
        
        }
    })
})

Result from result_arr

rows [
  { id: 5, name: 'wqeqwe', selected: 13, absent: 1 },
  { id: 7, name: 'weqe', selected: 3, absent: 1 },
  { id: 8, name: 'me', selected: 13, absent: 1 },
  { id: 9, name: 'joe', selected: 18, absent: 0 },
  { id: 10, name: 'tim', selected: 3, absent: 0 }
]

RemoveMax function will take out the highest selected (8) and shuffle the others (1, 3, 4, 6), then will catch a random one

function removeMax() {
    const max = Math.max(result_arr.map(item => item.selected));
    newArr = result_arr.filter(item => item.selected !== max)
    shufflearray = newArr[Math.floor(Math.random()*newArr.length)];
    setShuffle(shufflearray);
    hello();

}

This is connected with RemoveMax, but since its async I have to make it global to add it into another function.

var result_shuffle = [];
function setShuffle (shufflearray) {
    result_shuffle = shufflearray;
    return result_shuffle   
}

The shuffler has picked a random Selected.

result_shuffle { id: 5, name: 'wqeqwe', selected: 13, absent: 1 }

Coming to the question. wqeqwe is absent today, hence the execution must be called off, however, I still want someone who is NOT absent to be selected, and have the code roll again. How do I do this?

function hello() {
    console.log("1", result_shuffle);
    console.log("2", result_shuffle.absent);
    
    if (shufflearray.absent == 1) {
       const baddy = "urbad"

    }
    else (shufflearray) => {
        var sql = `UPDATE developers SET selected = ${result_shuffle.selected} + 1 WHERE id = ${result_shuffle.id}`;
        db.query(sql, function (err, result) {
            if (err) throw err;
            console.log(result.affectedRows + " record(s) updated");
        });
    }
}
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You just filter it. You're already doing that, you just need to filter it twice, once for each condition.

But first, Math.max expects a different input. You'll need to spread that array before it will work. And then I have just added a second filter to the newArr mapping remove everybody that's absent.

const result_arr = [
  { id: 5, name: 'wqeqwe', selected: 13, absent: 1 },
  { id: 7, name: 'weqe', selected: 3, absent: 1 },
  { id: 8, name: 'me', selected: 13, absent: 1 },
  { id: 9, name: 'joe', selected: 18, absent: 0 },
  { id: 10, name: 'tim', selected: 3, absent: 0 }
];

const max = Math.max(...result_arr.map(item => item.selected));
let newArr = result_arr.filter(item => item.selected !== max)
  .filter(item => !item.absent);

console.log(newArr);

But you also need to filter the absent people out of the max calculation, I think, in case one of the absent people has the highest selected, so I added the absent filter there also. Also, bear in mind that if joe (18) were absent and wqeqwe and me (13) were not, both wqeqwe and me would be removed, since they have the same selected.

const result_arr = [
  { id: 5, name: 'wqeqwe', selected: 13, absent: 1 },
  { id: 7, name: 'weqe', selected: 3, absent: 1 },
  { id: 8, name: 'me', selected: 13, absent: 1 },
  { id: 9, name: 'joe', selected: 18, absent: 0 },
  { id: 10, name: 'tim', selected: 3, absent: 0 }
];

const max = Math.max(
  ...result_arr.filter(item => !item.absent)
  .map(item => item.selected)
);

let newArr = result_arr.filter(item => item.selected !== max)
  .filter(item => !item.absent);

console.log(newArr);

Then you just shuffle that filtered array with your shuffle function.

You could also combine both conditions into the first filter:

const result_arr = [
  { id: 5, name: 'wqeqwe', selected: 13, absent: 1 },
  { id: 7, name: 'weqe', selected: 3, absent: 1 },
  { id: 8, name: 'me', selected: 13, absent: 1 },
  { id: 9, name: 'joe', selected: 18, absent: 0 },
  { id: 10, name: 'tim', selected: 3, absent: 0 }
];

const max = Math.max(
  ...result_arr.filter(item => !item.absent)
  .map(item => item.selected)
);

let newArr = result_arr.filter(
  item => item.selected !== max && !item.absent
);

console.log(newArr);

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda