Javascript, correct sequence of numbers [closed]

-2

I do not 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>
    
asked by anonymous 24.09.2014 / 18:44

2 answers

1

You had some errors in this code. From lenght badly typed, to fetch the input value.

I leave here the solution with the bugs fixed:

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

    
24.09.2014 / 18:56
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>
    
24.09.2014 / 19:06