Error on a JS Promise

Asked

Viewed 25 times

2

I have the following code:

var age = parseInt(prompt('Digite sua idade: '));

function verificar(age){
    return new Promise(function(resolve,reject){
        if(age > 18){
            resolve(console.log('DEU CERTO'))
        }else{
            reject(console.log('MENOR'))
        }
    })
}

verificar()
.then(
    function(){
        console.log('Maior de idade')
    }
)
.catch(
    function(){
        console.log('Menor de idade')
    }
)

I’ve already converted the prompt() for whole type using the parseInt(). The problem is that even typing an age greater than 18, it keeps falling into the .catch() and returning that the user is under age. Someone knows how to solve?

  • 2

    But you are not passing any parameter when you invoke the function verificar(). Shouldn’t be verificar(age)?

  • @user140828 was just that, I didn’t notice rs. Thank you.

  • some response helped you?

1 answer

2

You forgot to pass "act" as parameter when calling check(). By using the same name "age" as parameter name in function definition, the parameter "hid" the global variable.

Just change line 13 to

verificar(age)

and you will probably also want to test age using >= instead of >, because 18 years is already of age.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.