lundi 8 novembre 2021

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()

Aucun commentaire:

Enregistrer un commentaire