0

yI have an array that receives one new string element ever 2 seconds on a loop:

//tick world
setInterval(function(){
  doTradeUpdate();
},5000);

function doTradeUpdate(){
  var randyManu = Math.floor(Math.random() * 10);
  switch(randyManu){
  case 0:
  //new duro mine
  countPush(manufacture,"duro-mine");
  break;

  case 1:
  //new e-plant
    countPush(manufacture,"e-plant");
    break;
//etc
}

function countPush(arrayz,addable){
  console.log(arrayz);
  console.log("Attempting to add: " + addable);
  if(arrayz.length == 0){
    arrayz.push(addable);
  }
  else{
    if (arrayz.indexOf(addable)  > 0){
       console.log("FOUND");
    }
    else{
        console.log("NOT FOUND");
        arrayz.push(addable);
    }
  }
}

If I let this code run, sometimes the result will appear as FOUND and sometimes as NOT FOUND for the same array element eg: "e-plant". I can therefore end up with multiple entries that are the same within the array. Why then is my code not able to consistently match elements?

This is basically the whole script. Nothing else touches the array.

Many thanks!

G

2
  • 1
    tl;dr Array.indexOf() MDN Commented May 4, 2015 at 10:28
  • You are all right of course. Not sure quite what I was thinking. I've altered the test to >= 0 as of course I need to check index 0. Can someone assign me some numpty points or something? Thats what you get for having a bank holiday. Commented May 4, 2015 at 10:42

5 Answers 5

1

use

if (arrayz.indexOf(addable) !== -1)
{
  console.log("FOUND");
}
else
{
  console.log("NOT FOUND");
  arrayz.push(addable);
}

I.e. use (!== -1) instead of (> 0).
May be if the element is in position 0 it says Not FOUND. Also you can optimize as below

function countPush(arrayz,addable)
{
console.log(arrayz);
console.log("Attempting to add: " + addable);
if(Array.isArray(arrayz))
{
  if(arrayz.indexOf(addable) !== -1) {
    console.log('Exists and found');
  }
  else {
    arrayz.push(addable);
  }
}    
}

There are better ways to check if a variable is an array, i am just giving an example!
Another point, why do you want to send arrayz everytime if you are sending the same array in both calls? may be you can have it in a common outer namespace!

Sign up to request clarification or add additional context in comments.

3 Comments

@Mytri it cannot be integer because he is passing strings "only" as per his explanation. It is also the entire code so its not possible
I send arrayz because this is a multi-use function that accepts different arrays for different reasons.
ah right. i couldn figure that out in the above code since manufacture is not present anywhere else and also since you mentioned this is the entire code : )
1

try this

 if (arrayz.indexOf(addable)  > -1)
        {
            console.log("FOUND");
        }
        else
        {
            console.log("NOT FOUND");
            arrayz.push(addable);
        }

Comments

1

If the element is not found, then -1 will be returned, therefor your check will fail, because you check if the index is greater than 0 .

You need to check:

(arrayz.indexOf(addable) !== -1)

Comments

1

indexOf() returns -1 when it doesn't find the element. 0 means "I found the element in the first position of the array", which is clearly not what you want.

You should change

if (arrayz.indexOf(addable)  > 0)

to

if (arrayz.indexOf(addable) > -1) 

Or, even better, merge your ifs ending up with this:

function countPush(arrayz,addable) {
    console.log(arrayz);
    console.log("Attempting to add: " + addable);

    // Same as nested ifs, due to short-circuiting
    if(arrayz.length === 0 || arrayz.indexOf(addable) === -1) {
        console.log("NOT FOUND");
        arrayz.push(addable);
    } else {
        console.log("FOUND");
    }
}

1 Comment

I'd merge them but I have another condition to add later.
0

try this

(arrayz.indexOf(addable) !== -1)?(console.log("FOUND")):((console.log("NOT FOUND")) || (arrayz.push(addable))); console.log(arrayz);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.