-5

I have an array containing elements .

What I want is to count the no. of elements every time when the Set Interval function loads in javascript.

Something like a counter but when I am using the count function it's displaying an error :

Uncaught TypeError: x.count is not a function

I just want to count the no. of values when setinterval function occurs every time starting from 0 .

Here is the Code :

var countArray = ["2020-14-03 11:14:48.225000","2020-14-03 11:14:48.226000","2020-14-03 11:14:48.227000","2020-14-03 11:14:48.228000","2020-14-03 11:14:48.229000","2020-14-03 11:14:48.230000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.232000","2020-14-03 11:14:48.233000","2020-14-03 11:14:48.234000","2020-14-03 11:14:48.235000","2020-14-03 11:14:48.236000","2020-14-03 11:14:48.237000","2020-14-03 11:14:48.238000","2020-14-03 11:14:48.239000","2020-14-03 11:14:48.240000","2020-14-03 11:14:48.241000","2020-14-03 11:14:48.242000","2020-14-03 11:14:48.243000","2020-14-03 11:14:48.244000"];

console.log(countArray.length);


var j = 0;

function countval() {
  return countArray[j++];
}

setInterval(function() {
  var counter = 0;
  x = countval();
  console.log("X value: ", x);
  counter = x.count();
  console.log("Count: ", counter);
}, 1000);

for example: console.log("Count: ", counter); should print:

0
1
2
3
4
5
6
7
8
9

after each second.

Update: My queston is of string format. how so I want to count the no. of string elements occurence in an array.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Chempooro
  • 415
  • 1
  • 7
  • 24

3 Answers3

1

Your problem is solved with this code

var countArray = ["2020-14-03 11:14:48.225000","2020-14-03 11:14:48.226000","2020-14-03 11:14:48.227000","2020-14-03 11:14:48.228000","2020-14-03 11:14:48.229000","2020-14-03 11:14:48.230000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.232000","2020-14-03 11:14:48.233000","2020-14-03 11:14:48.234000","2020-14-03 11:14:48.235000","2020-14-03 11:14:48.236000","2020-14-03 11:14:48.237000","2020-14-03 11:14:48.238000","2020-14-03 11:14:48.239000","2020-14-03 11:14:48.240000","2020-14-03 11:14:48.241000","2020-14-03 11:14:48.242000","2020-14-03 11:14:48.243000","2020-14-03 11:14:48.244000"];

console.log(countArray.length);


var j = 0;

function countval() {
  return countArray[j++];
}

setInterval(function() {
if(j <= countArray.length){
  var counter = 0;
  console.log("Count: ", j);
  console.log("X value: ", countval());
}
}, 1000);
1

Your script will be like this:

  var countArray = ["2020-14-03 11:14:48.225000","2020-14-03 11:14:48.226000","2020-14-03 11:14:48.227000","2020-14-03 11:14:48.228000","2020-14-03 11:14:48.229000","2020-14-03 11:14:48.230000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.232000","2020-14-03 11:14:48.233000","2020-14-03 11:14:48.234000","2020-14-03 11:14:48.235000","2020-14-03 11:14:48.236000","2020-14-03 11:14:48.237000","2020-14-03 11:14:48.238000","2020-14-03 11:14:48.239000","2020-14-03 11:14:48.240000","2020-14-03 11:14:48.241000","2020-14-03 11:14:48.242000","2020-14-03 11:14:48.243000","2020-14-03 11:14:48.244000"];

  console.log(countArray.length) ;


var j = 0;
  function countval() 
  {
   return countArray[j++];
  }

setInterval(function(){
  var counter = 0;
  x= countval();
  console.log("X value: ", x);
  counter=countArray.indexOf(x);
console.log("Count: ", counter);
  }, 1000); 
Arijit Jana
  • 220
  • 1
  • 4
1

The error is quite simple. You're assigning x = countVal(); and countVal() returns a string value, so basically you're doing this: "2020-14-03 11:14:48.225000".count(); which is an incorrect statement. So, Firstly remove this line from your code. Other than that I'm having difficulty understanding what you're trying to do, from reading you're question I assumed you want something like this:

setInterval(function(){
    if(j < countArray.length){
        x = countval(); 
        console.log("X value: ", x);
        console.log("Count: ", j);
    }
}, 1000);

And if you want to show the length of each element of the array just edit your .count() to .length

Saud
  • 859
  • 1
  • 9
  • 23