Ad

In this kata there will be two inputs.Your task is to determine whether or not they are relative numbers. If they are relative numbers then return the sring "relative" and if they are not return the string "non relative".

Relative numbers are integers that have no common divisors other than 1 and themselves.

Examples:

-> 8 (divisors: 1, 2, 4, 8) and 9 (divisors: 1, 3, 9) are relative numbers since they do not have common divisors that are not 1 or themselves.

-> 1 (divisors: 1) and 9 (divisors: 1, 3, 9) are relative numbers since they do not have common divisors that are not 1 or themselves.

-> 3 (divisors: 1, 3) and 3 (divisors: 1, 3) are relative numbers since they do not have common divisors that are not 1 or themselves.

-> 9 (divisors: 1, 3, 9) and 9 (divisors: 1, 3, 9) are not relative numbers since they do have a common divisor (3) that is not 1 or themselves.

-> 9 (divisors: 1, 3, 9) and 21 (divisors: 1, 3, 7, 21) are not relative numbers since they do have a common divisor (3) that is not 1 or themselves.

Code
Diff
  • function relativeNumbers(a, b) {
      for (let i = 2; i <= Math.min(a, b)**(1/2); i++) {
        if (a % i === 0 && b % i === 0) {
          return "non relative";
        }
      }
      return "relative";
    }
    • function relativeNumbers(a, b) {
    • let arrOne = [];
    • let arrTwo = [];
    • for (let i = 2; i < 10; i++) {
    • if (a % i === 0 && a !== i) {
    • arrOne.push(i);
    • }
    • if(b % i === 0 && b !== i){
    • arrTwo.push(i)
    • }
    • }
    • for(let i = 0; i < arrOne.length; i++){
    • for(let j = 0; j < arrTwo.length; j++){
    • if(arrOne[i] === arrTwo[j]){
    • return "non relative"
    • }else return "relative"
    • for (let i = 2; i <= Math.min(a, b)**(1/2); i++) {
    • if (a % i === 0 && b % i === 0) {
    • return "non relative";
    • }
    • }
    • return "relative";
    • }