Wednesday, 15 February 2012

finding the nth prime javascript -


The tasks given below should exit the nth prime number. However, it stays out spitting 3. Can anyone help? Cheers, anthony

  function prime (num) {output = true for (i = 2; i & lt; num; i ++) {if (num% i === 0) { Output = false; Break}} return output} function primemover (num) {var count = 0 (i = 2; i & lt; 10000; i ++) {if (main (i) === true) {count = count + 1} === Points) {Return break I}}}  

You made a counter counter i in global scope . Therefore, both PrimeMover and major change the same global i . At each instance, assigns the primemover i = 2 . After this, Prime assigns i = 2 . You can change the value of i variable 2 and 3 .use local loop counter variable var i = 0;

  will change between the function {// var to {output = true (var i = 2; i & lt; num; i ++) {// var I = 2 if (num% i === 0) {output = false; Break}} return output} function primemover (num) {var count = 0 (var i = 2; i & lt; 10000; i ++) {// var i = 2 if (prime (i) === true) {Count = counting + 1} if (count === number) {return break i}}}  

No comments:

Post a Comment