-2

I want to list all of the factors and prime factors of two set of numbers.

Here is my code, but my prime factors are not right and the comma at the end of the lines are not needed.

Any help to correct the prime factor? 4 should be 2,2,2 but it comes to be 2.

var l = parseInt(prompt("What is your lowest number"));
var k = parseInt(prompt("What is your highest number"));
var results = '';

//function counts (incremental) the numbers
// display the numbers in bold
// numbers are display on the left of

    function genFactors(num) {
    var result = "";
    var result1 = "";

    for (var i = 2; i <= num; ++i) {
        if (num % i == 0) {
            result += i + ",";
            result1 += genPrimeFactors(i);
        }
    }
    results += '<b>' + num + '</b>: ' + result1 + '<br>';

    return result;
    }

    document.writeln("<h1>All Factors</h1>");
    document.writeln("<p>The factors of " + l + " and " + k + " are: " + ":      
    </p>");

    for (var num = 1; num <= k; ++num) {
    document.writeln("<b>" + num + "</b>" + ":" + genFactors(num) + "<br />");
   }
//  determine the prime numbers


    function isPrime(num) {
    for (var i = 2; i < num; i++) {
        if (num % i === 0) {
            return 0;
        } else {
            return 1;
        }
    }
    }
//determine the prime factors

     function genPrimeFactors(num) {
     for (var i = 2; i <= num; i++) {
        if (num % i != 0) {
            while ((num / 2 == parseInt(num / 2)) && (num > 2)) {
                num = num / 2;
                return "";
            }
            if (isPrime(num)) {
                return num + ',';
            } else {

            }
        }
      }
      if (num == 2) {
        return 2 + ',';
    }
    }

/*function 
    function genPrimeFactors(num) {
    var result = "";

    for (var i = 2; i <= num; ++i) {
        for (var k = 2; k <= i; ++k) {
            if (i % k != 0) {
            }
            result += i + ",";
        }

    }
    return result;
}
*/output

    document.writeln("<h1>Prime Factors</h1>");
    document.writeln("<p>The prime factors of " + l + " and " + k + " are: " + ":</p>");
   document.writeln(results);
  • Possible duplicate of [Javascript chop/slice/trim off last character in string](http://stackoverflow.com/questions/952924/javascript-chop-slice-trim-off-last-character-in-string) – SlashmanX Feb 25 '16 at 10:55
  • OP isn't only asking to remove comma. Read the question. – Chris Feb 25 '16 at 10:56
  • Start by fixing `isPrime()` (check it with num=9) – georg Feb 25 '16 at 10:58

1 Answers1

0

You can cut out the comma with substring at the return, like this

function genFactors(num) {
 var result = "";
 var result1 = "";

 for (var i = 2; i <= num; ++i) {
     if (num % i == 0) {
         result += i + ",";
         result1 += genPrimeFactors(i);
     }
 }
 result1.substring(0,result1.length-1);//maybe try this
 results += '<b>' + num + '</b>: ' + result1 + '<br>';

 return result.substring(0,result.length-1);
}
luk492
  • 350
  • 3
  • 15
  • Thanks luk492. I fixed the factors, but the prime factor is still a struggle. I edited the strings but not working for the prime factors. Any help? – Karel Poborski Feb 25 '16 at 11:53
  • @Karel Poborski I edited the answer. After you get the results you want you use the same substring. – luk492 Feb 25 '16 at 13:02
  • That was my understanding, but still not working @Luk492. Sorry I am new to program (javascript) – Karel Poborski Feb 26 '16 at 00:48
  • /*function function genPrimeFactors(num) { var result = ""; for (var i = 2; i <= num; ++i) { for (var k = 2; k <= i; ++k) { if (i % k != 0) { } result += i + ","; } } return result.substring(0,result.length-1); – Karel Poborski Feb 26 '16 at 00:52