lundi 29 décembre 2014

why the if statements are not working in javascript


I have a simple javascript below, but it seems the if statement is not executed correctly that depends on which statement is put in last, the result will be either n+2 or n*2 while x%2 is always 0:



<html>
<script>

var x=10;
while (x<2000) {
if (x%2==0) function collatz(n) {return n+2;}
if (x%2==1) function collatz(n) {return 2*n;}

x=collatz(x);
document.write(x, ",");
}

</script>
</html>




Aucun commentaire:

Enregistrer un commentaire