OneCompiler

find the prime number in an matrix (multidimensional array)

173

N = 3 M = 3

arr = [[1 2 3]
[4 5 6]
[7 8 9]]

solution

//starting from here && code here
function checkprime(n){
var count =0;
for(var i=1; i<=n; i++){
if(n%i==0){
count++;

      }
  }if(count==2){
      return true;
  }else{
      return false;
  }

}
var count = 0;
for(var i = 0; i<N; i++){
for(var j=0; j<M; j++){
if(checkprime(arr[i][j])){
count++;
}
}
}
console.log(count);
}