2
"Create a function that receives a user’s age and returns a Promise that after 2 seconds will return if user is older than 18 years or not"
When I open the page after 2 seconds the result loads even without me having entered a value. And right after, when I put a value (higher or lower than 18) the result always says that it is lower.
My code:
function clicado() {
var inputIdade = document.querySelector('input.idad').value;
return inputIdade;
}
function checaIdade(idade) {
return new Promise(function(resolve,reject){
setTimeout (function() {
if(idade > 18) {
resolve();
} else {
reject();
}
}, 2000 );
})
}
checaIdade(clicado())
.then(function() {
console.log("Maior que 18");
})
.catch(function() {
console.log("Menor que 18");
});
Welcome to Stack Overflow in English :-) ... But what’s the problem with your code? Is there an error? More details on your question.
– Luiz Felipe
Thanks :) So when I open the page after 2 seconds the result loads even without I have entered a value. And right after, when I put a value (higher or lower than 18) the result always says that it is lower.
– Nico