Meet a strange phenomenon of JavaScript Array.indexOf (in Node.js) -


when try solve projecteuler no.27, met strange problem of javascript array.indexof (in node.js). code below:

var primes = require('./primes1000.js'); var bigprimes = require('./primes50000.js'); var maxnumber = 2; var maxpair = {};  var getquadratic = function(b) {     var = 0;     var = 0;     while (math.abs(a) < 1000) {         var prime = primes[i];         var = prime - 1 - b;         var length = getmaxnumber(a, b) - 1;         if (length > maxnumber) {             maxnumber = length;             maxpair = {                 a: a,                 b: b             };             console.log(maxpair, maxnumber);         }         i++;     } };  var getmaxnumber = function(a, b) {     var isprime = true;     var n = 1;     while (isprime) {         n = n + 1;         var result = (n + a) * n + b;          isprime = (bigprimes.indexof(result) !== -1);         // if ((a === 1) & (b === 41)) {         //     console.log(result, isprime);         // }     }      return n; };  (var = 0; < primes.length; i++) {     getquadratic(primes[i]); } 

primes1000.js , primes50000.js written this:

module.exports = [2,3,5,7,11,13,17,...,997]; module.exports = [2,3,5,7,11,13,17,...,611953]; 

the problem occurs at

bigprimes.indexof(result) 

when result = 47, indexof method returns -1, means not found.

after lot debugging , testing, @ last changed node to

bigprimes.indexof(parseint(result)) 

then works.

but result of

typeof result 

and

typeof bigprimes[14] 

are both "number" and

parseint(result) === result 

returns true.

i didn't see why have parseint correct return value of indexof.

your result float reason. javascript has 1 number class. it's not language multiplying, dividing. guess it's not bad thing cast int. don't think there's specific reason why happens.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -