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

188
Visualizações
Function getRandomIntInclusive(min, max) am I using correctly

In the code below I am trying to have the computer randomly choose a button and change the color opacity. Based on the random number generated: if random number === 1 change red color and so on. However, when I run this code. I randomly get an undefined. To troubleshoot this I added the console.log('Something is not working') to try and figure it out. My question is if I am using the random function correctly ( which I believe I am, why is is getting to the point of 'Something not working'. The random numbers should be between 1 and 4.

    let randomColor = function getRandomIntInclusive(min, max){
        min = Math.ceil(min)
        max = Math.floor(max)
        console.log(Math.floor(Math.random() * (max - min +1)+ min))
    }
    function getRandomColor() {
        if (randomColor(1, 4) === 1){
            redBtn.style.opacity = "90%"
            setTimeout(function(){
                redBtn.style.opacity = "40%"
            },300)
            console.log(randomColor)
        } 
    
        else if (randomColor(1, 4) === 2){
            blueBtn.style.opacity = "90%"
            setTimeout(function(){
                blueBtn.style.opacity = "40%"
            },300)
            console.log(randomColor)
        } 
    
        else if (randomColor(1, 4) === 3){
            greenBtn.style.opacity = "90%"
            setTimeout(function(){
                greenBtn.style.opacity = "40%"
            },300)
            console.log(randomColor)
        } 
    
        else if (randomColor(1, 4) === 4){
            yellowBtn.style.opacity = "90%"
            setTimeout(function(){
                yellowBtn.style.opacity = "40%"
            },300)
           console.log(randomColor)
        } 
        else console.log('Something is not working')
        console.log(randomColor)
    
    }
    getRandomColor()
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First, you were not returning anything from your function getRandomIntInclusive, you were just printing the output in the console. If you don't return anything, it will obviously get value undefined.

Then, instead of calling the function in every if statement, call the function once, keep the value in a variable then apply your necessary logic.

var redBtn = document.getElementById("redBtn");
var blueBtn = document.getElementById("blueBtn");
var greenBtn = document.getElementById("greenBtn");
var yellowBtn = document.getElementById("yellowBtn");

let randomColor = function getRandomIntInclusive(min, max){
        min = Math.ceil(min);
        max = Math.floor(max);
        console.log(Math.floor(Math.random() * (max - min +1)+ min));
        //return the value 
        return Math.floor(Math.random() * (max - min +1)+ min);
};
    function getRandomColor() {
    
    //getting the value
    var randomValue = randomColor(1, 4);
    
        if (randomValue === 1){
            redBtn.style.opacity = "90%";
            setTimeout(function(){
                redBtn.style.opacity = "40%"
            },300);
            console.log(randomColor);
        } 
    
        else if (randomValue === 2){
            blueBtn.style.opacity = "90%";
            setTimeout(function(){
                blueBtn.style.opacity = "40%"
            },300);
            console.log(randomColor);
        } 
    
        else if (randomValue === 3){
            greenBtn.style.opacity = "90%";
            setTimeout(function(){
                greenBtn.style.opacity = "40%"
            },300);
            console.log(randomColor);
        } 
    
        else if (randomValue === 4){
            yellowBtn.style.opacity = "90%";
            setTimeout(function(){
                yellowBtn.style.opacity = "40%"
            },300);
           console.log(randomColor);
        } 
        else {
           console.log('Something is not working');
        }
        console.log(randomColor)
    
    };
    
getRandomColor();
<button id="redBtn">Red Button</button>
<button id="blueBtn">Blue Button</button>
<button id="greenBtn">Green Button</button>
<button id="yellowBtn">Yellow Button</button>

Note: These are some dummy buttons I created for testing purposes, please ignore that part.

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