Javascript, hit sequence of numbers

Asked

Viewed 998 times

-2

I don’t know why it’s not working:

<input type="text" id="numero"/>
<input type="submit" id="adivinha" value="compare com meu segredo"/>
<script>
var segredo=[1,2,5,6,11,12,15,16];
var numero=document.getElementById("numero").value;
var botaoclicado=function() {
for(var i=0;i<segredo.lenght;i=i+1){
if(segredo[i]==numero){
alert("parabens, acertou");
return;
}
}
alert("errou");
};
var botaoadivinha=document.getElementById("adivinha");
botaoadivinha.onclick=botaoclicado;
</script>
  • You have to make it clearer what you want?

  • I think it could be much better in the explanation so it gets easier

2 answers

1

There were some errors in that code. Since lenght misspelled, fetching input value.

I leave here the solution with the corrected errors:

var botaoclicado=function() {
    var segredo= new Array(1,2,5,6,11,12,15,16);
    var numero=document.getElementById("numero").value;
    for(var i=0;i<segredo.length;i++){
        if(segredo[i]==numero){
            alert("parabens, acertou");
            return;
        }
     }
     alert("errou");
};
var botaoadivinha=document.getElementById("adivinha");
botaoadivinha.onclick=botaoclicado;

LINK here

0

"lenght" is actually "length", see:

<input type="text" id="numero"/>
<input type="submit" id="adivinha" value="compare com meu segredo"/>
<script>
var segredo=[1,2,5,6,11,12,15,16];    
var botaoclicado=function() 
{    
    var numero=document.getElementById("numero").value; 
    for(var i=0;i < segredo.length;i=i+1){

        if(segredo[i]==numero){
            alert("parabens, acertou");
            return true;
        }
    }    
    alert("errou");
};
var botaoadivinha=document.getElementById("adivinha");
botaoadivinha.onclick=botaoclicado;
</script>
  • Oblivious, but I have one more question why I need to put the true?

Browser other questions tagged

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