0

I am currently trying to get my program to work. It is supposed to factor numbers from a low to high bound. In other words, if the low bound is 12, and the high bound is 16, it should output the following into a tag....

12: 2,3,4,6,12
13: 13
14: 2,7,14
15: 3,5,15
etc.....

However, I am getting the output like this:

12: 2,3,4,6,12,
13,
2,7,14,
3,5,15,
2,4,8,16,
17,

it is outputting in a strange manner, and I keep trying to move it around. The only line that is correct is the top one. Can anyone here give me a hand? I am sure it's something minor, but I just can't get it......

    function calculate(num){
    var int = 2;    
    var num = document.getElementById("num").value;
    var high = document.getElementById("high").value;
    var str = num + ": ";


    while (num <= high){

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

    }   
    num++;

    str += "\n";
    }
    document.getElementById("outputArea").innerHTML = str;
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
user3371104
  • 269
  • 2
  • 3
  • 6

3 Answers3

0

You are only added the num + ': ' once. Change it to this:

function calculate(num) {
    var int = 2;
    var num = document.getElementById("num").value;
    var high = document.getElementById("high").value;
    //var str = num + ": "; // this doesn't go here.

    while (num <= high) {
        var str = num + ": "; // this goes here
        for (var i = 2; i <= num; i++) {
            if (num % i == 0) {
                str += i + ",";
            }

        }
        num++;

        str += "\n";
    }
    document.getElementById("outputArea").innerHTML = str;
}
Bic
  • 3,141
  • 18
  • 29
0

Your num + ': ' was in the incorrect positon. Also, you will always print a comma, which you shoudln't do unless there are more than one. The \n won't work since you are displaying it with HTML so I changed it to <br>

Here's my updated fucntion:

function calculate() {
    var int = 2;
    var num = document.getElementById("num").value;
    var high = document.getElementById("high").value;
    var str = "";


    while (num <= high) {
        str += num + ": ";
        var first = true;
        for (var i = 2; i <= num; i++) {
            if (num % i == 0) {
                str += ((!first) ? ", " : "") + i;
                first = false;
            }
        }
        num++;
        str += "<br>";
    }

    document.getElementById("outputArea").innerHTML = str;
}

JSFiddle: http://jsfiddle.net/howderek/Bfeva/

howderek
  • 2,224
  • 14
  • 23
0

You have to define str outside while loop and do:

  str += num + ": ";

inside while. Like in this DEMO.

var str = "";
while (num <= high) {
    str += num + ": ";
    for (var i = 2; i <= num; i++) { 
        if (num % i === 0) {               
            str += i + ",";
        }
    }
    num++;
    str += "\n";
}

Plus

If you don't want the last comma, you have to add this check inside for loop:

str += i;
if (i != num) {
  str += ",";
}
SirDeveloper
  • 276
  • 1
  • 10