-1

I am trying to use this code in Javascript to verify prime numbers using an array to store them:

var nprimi = [2];

function verifica(x)
{ 
var i;
var x;
var k;

 //for (k in nprimi)
 for (k=0;k<nprimi.length;k++)
    {
    if (nprimi[k]==x)
       return true;
    else if (x%nprimi[k]==0)
       return false;
    }

for (i=k+1;i<x;i++)
   {
   if (x%i==0)
      return false;
   }

 nprimi.push(x);
 return true;
 }

My problem is:

If I use

 for (k=0;k<nprimi.length;k++)

for loop is running correctly, but using

 for (k in nprimi) 

doesn't work.

It seems that after each nprimi.push(x) the number of elements in objetc nprimi is always zero.

Maybe it is a very stupid error, but I am not able to find it! Thank you very much for helping!

Kian
  • 1,319
  • 1
  • 13
  • 23
  • 1
    "doesn't work" is not a useful problem description. What do you expect, and what happens instead? – T.J. Crowder May 02 '16 at 12:48
  • 2
    Why are you trying to use `for-in` on an array? It's for looping through the enumerable property names of an object, not the indexes of an array. More about looping arrays [in this answer](http://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript/9329476#9329476). – T.J. Crowder May 02 '16 at 12:48
  • please check this [URL](http://stackoverflow.com/tour) ,it will be help you to raise your content quality – Willie Cheng May 02 '16 at 12:53

4 Answers4

0

It's not working because that's not what for-in is for. for-in loops through the names of enumerable object properties, not array indexes. Those names are strings, not numbers. So following your loop, k+1 will be a concatenation operation, not an addition operation. That is, if k is "2", then k+1 will be "21".

Don't use for-in to loop through arrays unless you know what you're doing and use safeguards. More about looping arrays in this answer.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Ciao Giancarlo, the short answer is: in JavaScript you do not use for..in to loop through arrays.

You use for..in to loop through objects.

A really good explanation is to be found here.

Edit:

Another thing. In your code, in the function verifica, you have an x parameter and an x local variable, you should really remove the var x declaration.

function verifica(x)
{ 
  var i;
  var x; // <-- this shouldn't be here.
Community
  • 1
  • 1
Alberto Chiesa
  • 7,022
  • 2
  • 26
  • 53
0

In for loop after the loop the value of k will be incremented (k++). But in the case of for-in it will not happen. So in that case you have to do it manually. I have updated the function:

function verifica(x)
{ 
var i;
var x;
var k;

 for (k in nprimi)
 //for (k=0;k<nprimi.length;k++)
    { 
    if (nprimi[k]==x)
       return true;
    else if (x%nprimi[k]==0)
       return false;
    }
 k++; //This is the line making difference in for-in loop
for (i=k+1;i<x;i++) {
   if (x%i==0)
      return false;
   }

 nprimi.push(x);
 return true;
 }
Aju John
  • 2,214
  • 1
  • 10
  • 27
0

for in iterates over the indexes of the array

var myArray = ['a', 'b', 'c'];
for (k in myArray) {
     console.log(k + ' -> ' + myArray[k]); 
}

outputs:

 0 -> 'a'
 1 -> 'b'
 2 -> 'c'

As you can see, the k variable does not contain the 'a', 'b', 'c'

Bruno
  • 252
  • 1
  • 8