-1

I want to build a function to know how many prime numbers there are in my matrix (5) and I want to return the quantity - 5

Can anyone understand where my mistakes are? At the end there is a mistake for sure, because I was unable to return the quantity

let matrix = [ [50,17], [19,20,6,40,14], [97] ,[31,9,5] ]; 

let counter = 0

for(let row = 0 ; row < matrix.length ; row++){

    for(let col = 0 ; col < matrix[row].length ;col++){

        function matrixPrimeNumbers(num){

            for(let d = 2 ; d<= num -1 ; d++) {

                if(num % d == 0){

                    return false;
                }
                num++;
                return num.charAt();
           }
       }
   }
}
sauhardnc
  • 1,961
  • 2
  • 6
  • 16
HardBeard
  • 47
  • 3
  • 12

2 Answers2

1

First of all, you should not declare a function in a for loop. I don't think that this throw an error but with some text editor, it probably shows a warning.

You can modify this method (in ES6 syntax) to check if anumber is a prime number, and returns (0 or 1) instead of (true or false).

// This method returns 1 if the number is a prime number, else return 0 
const isPrime = num => {
    for(let i = 2, s = Math.sqrt(num); i <= s; i++)
        if(num % i === 0) return 0; // Not a prime number, return 0
    return ( (num > 1) ? 1 : 0 ); // Return 1 if num > 1, else return 0
}

You can now use the isPrime function in your code like this :

let matrix = [ [50,17], [19,20,6,40,14], [97] ,[31,9,5] ]; 

let counter = 0

for(let row = 0 ; row < matrix.length ; row++){

    // Counter will increase if isPrime returns 1
    for(let col = 0 ; col < matrix[row].length ;col++)
        counter += isPrime(matrix[row][col])

}

console.log(counter); // Should show in console how many prime numbesr you have in the matrix
Dony
  • 1,543
  • 2
  • 10
  • 25
1

There are many issues:

  • The function matrixPrimeNumbers is never executed. In your nested loop, you do nothing else then define the same function over and over again. The function is never invoked.

  • The function itself has a disfunctional loop: on the very first iteration of that loop, it will exit at the return statement. So there is actually no looping.

  • Calling num.charAt() is ... strange? charAt is a string method, and returns a character. Why would you want to call it on a number, and what does that have to do with finding a prime? Elsewhere you have return false, so presumably you want the function to return a boolean. True when num is prime, and false when it is not. So after the loop finishes, just add return true.

  • num++; is wrong. You want to determine whether num is a prime, so you should not change its value during the loop. Probably you intended to do counter++;, but it is placed at the wrong spot. This should only be done when you are certain there is no divisor, and so it should be placed after the loop. Better still, it should happen outside the function, after a call of that function.

  • You say you want to "return the quantity". But you never return the counter. In order to return something, you need a function for it. So wrap most of the code into a function, and let it return the counter;

  • It is OK to have a function for detecting whether num is a prime, but then name it accordingly, like isPrime. Use the name matrixPrimeNumbers for the main function that will do the nested looping over the matrix. I would even change the name to something more telling: countMatrixPrimeNumbers.

  • It is not necessary for prime detection to loop to num-1. You can stop at the square root of num.

  • Integers that are 1 or less are not considered primes, so you should deal with that case.

So here is a correction:

let matrix = [ [50,17], [19,20,6,40,14], [97] ,[31,9,5] ]; 

// Define functions at the top level, not inside for-loops
function isPrime(num) {
    if (num <= 1) return false;
    let root = Math.sqrt(num);
    for (let d = 2; d <= root; d++) {
        if (num % d == 0) {
            return false;
        }
    }
    return true;
}

function countMatrixPrimeNumbers(matrix) {
    let counter = 0

    for (let row = 0; row < matrix.length; row++) {
        for (let col = 0; col < matrix[row].length; col++) {
            if (isPrime(matrix[row][col])) {
                counter++;
            }
        }
    }
    return counter;
}

// call it:
console.log(countMatrixPrimeNumbers(matrix));
trincot
  • 317,000
  • 35
  • 244
  • 286