K
K
Kusmich2015-10-18 21:57:20
JavaScript
Kusmich, 2015-10-18 21:57:20

Why doesn't a simple function work in a loop?

There is a function that takes , splits an array of numbers into numbers and also drives it into an array, then takes the first number of the array and makes a miscalculation to it. For example, the number in the array is 2 . Counts 1, 2. etc . The bottom line is that the arrays on which the miscalculation is done change. every 10 sec. There are only 5 of them. And the function that makes this calculation works only 4 times. Tell me what's wrong? Incorrectly declared or incorrectly called?

here is the code:

var numbers = [27389, 23948, 59835, 39843, 17263];
var stnum_1;
function replaceNum(i) {
    if (i < 0 || i >= numbers.length) {
        return;
    
    };
    var digits = (numbers[i] + "").split("");
    console.log(digits);
  stnum_1 = digits[0];

    setTimeout(function() { replaceNum(i + 1);checNum(); }, 2000);
  
};
replaceNum(0);

var StateStnum_1 = 0;

function checNum(){
  
while (StateStnum_1!=stnum_1) {
  StateStnum_1++;
  if(StateStnum_1 > 9){StateStnum_1 = 0;}
  console.log(StateStnum_1);
  
  
  
};
};
checNum();


It is the function checkNum(); works out the current 4 times and it needs 5 after each array change

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Curly Brace, 2015-10-18
@Kusmich

checkNum(); replaceNum(i + 1); checkNum();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question