0

The .length and values of the array however are randomly assigned. More specifically, a() randomly assigns anArray.length to a random value 1-3, and the values therein are also randomly assigned a value 1-3. I've coded numTester() to test for 2 within anArray after a() is executed.

var anArray = [];
var numTester = function(aryParam, v) {

  var i;
  for (i = 0; i < aryParam.length; i++) {

    if (aryParam[i] == v) {

      return true;

    }

    return false;

  }

};
var a = function() {

  b = 0;

  c = setInterval(function() {

    b++;
    d = Math.floor(Math.random() * 3) + 1;
    anArray.push(d);

    if (b == d || b == 3) {

      clearInterval(c);
      document.write(anArray.join(", "), "<br />", numTester(anArray, 2));

    }

  }, 1);

};
a();

numTester() is supposed to return true if 2 is any of 1, 2, or 3 numbers within the array. The issue is that I've found that it will only return true if the first number of the array is 2. Why is this?

var anArray = [];
var numTester = function(aryParam, v) {

    var i;
    for(i = 0; i < aryParam.length; i++) {

        if(aryParam[i] == v) {

            return true;

        }

        return false;

    }

};
var a = function() {

    b = 0;

    c = setInterval(function() {

        b++;
        d = Math.floor(Math.random() * 3) + 1;
        anArray.push(d);

        if(b == d || b == 3) {

            clearInterval(c);
            document.write(anArray.join(", ") + "<br />", numTester(anArray, 2));

        }

    }, 1);

};
a();
1
  • Looks like a use case for Array.prototype.some: anArray.some(x => x == v). ;-) Commented Jan 15, 2018 at 5:09

3 Answers 3

1

You return false in the for loop, your loop will only ever iterate once, need to move it outside the for loop.

var numTester = function(aryParam, v) {

    var i;
    for(i = 0; i < aryParam.length; i++) {
        if(aryParam[i] == v) {
            return true;
        }

        return false; <-- remove this
    }
    return false; <-- put it here
}
Sign up to request clarification or add additional context in comments.

1 Comment

jeez. now I feel dumb lol. That was actually quite obvious. Thanks a lot Heinrich.
1

Yes, have a good look at your for loop:

  for (i = 0; i < aryParam.length; i++) {

    if (aryParam[i] == v) {

      return true;

    }

    return false;

  }

The code in its body is:

if (aryParam[i] == v) {

  return true;

}

return false;

Which means it will either return true or false on its first iteration.

The solution:

Move the return false out of your loop:

  for (i = 0; i < aryParam.length; i++) {

    if (aryParam[i] == v) {

      return true;

    }


  }
  return false;

Comments

0

you can use array.find

var numTester = function(aryParam, v) {
  return aryParam.find((ar) => ar == v) && true || false;
};

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.