Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda