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

178
Vistas
How to take the result of two randomly generated values from two functions and add them together?

Both the first and second functions (n1 and n2) output a random number from their respective arrays into the console. Issues arise when I try to add these two generated values together and have the result displayed in the console as well. I tried creating a global variable to add the results but it did not work.

If n1() outputs 3 and n2() outputs 1, I would like the console to output 4.

Here is my code:

var add = (n1() + n2());

function n1() {
  var cpuSelection =  [1, 2, 3];
  var randomSelection = cpuSelection[Math.floor(Math.random() * cpuSelection.length)];
  console.log(randomSelection)
}
    
n1();

function n2() {
  var cpuSelection =  [1, 2, 3];
  var randomSelection = cpuSelection[Math.floor(Math.random() * cpuSelection.length)];
  console.log(randomSelection)
}
        
n2();

console.log(add)

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

console.log( (n1() + n2()));

function n1() {
  var cpuSelection =  [1, 2, 3];
  var randomSelection = cpuSelection[Math.floor(Math.random() * cpuSelection.length)];
  console.log(randomSelection)
return randomSelection;
}
    

function n2() {
  var cpuSelection =  [1, 2, 3];
  var randomSelection = cpuSelection[Math.floor(Math.random() * cpuSelection.length)];
  console.log(randomSelection)
return randomSelection;
}
        
about 4 years ago · Juan Pablo Isaza Denunciar

0

Like DM says, you must return those random values.

function n1() {
  var cpuSelection =  [1, 2, 3];
  var randomSelection = cpuSelection[Math.floor(Math.random() * cpuSelection.length)];
  console.log(randomSelection)
  return randomSelection;
}
    
n1();

function n2() {
  var cpuSelection =  [1, 2, 3];
  var randomSelection = cpuSelection[Math.floor(Math.random() * cpuSelection.length)];
  console.log(randomSelection)
  return randomSelection;
}
about 4 years ago · Juan Pablo Isaza 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