jeudi 29 juin 2017

What is wrong with my code?

I have this exercise about writing a javascript code which takes as input 2 numbers from a form. The first number should have only one digit. It has to show how many times is the first number repeated in the second one.

I have written this code and it doesn't work. Can someone please help me find what is wrong here?

<html>
<head>
<title>Vektor</title>
<script type = "text/javascript">

function validate(num){
if(num>1){
if(num<9){
return true;
}
}
return false;
}

function calculate(){
var n1, n2;
n1 = numra.nr1.value;
n2 = numra.nr2.value;
nr = parseInt(n1)
if(validate(nr)){
var count = 0;
for (int i = 0; i<n2.length; i++){
var n = n2.charAt(i);
if(n1==n){
count++;
}
}
numra.heret.value = count;
}
else{
alert("Gabim");
}
}

</script>
</head>
<body>
<form name = "numra" onsubmit = "calculate()">
<input type = "text" name = "nr1"><br>
<input type = "text" name = "nr2"><br>
<input type = "submit" name="llogarit" value = "llogarit"><br>
<input type = "text" name = "heret">
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire