JavaScript factoring

Question:

How to factor numbers in this form?

26 | 2
13 | 3
1  | 

Here is my still incomplete code: JSFIDDLE

Answer:

Following the factorization logic I arrived at this function:

function Calcular(nr) {
    var partes = [];
    while (nr > 1) {
        for (var i = 2; i <= nr; i++) {
            if (nr % i) continue;
            partes.push([nr, i]);
            nr = nr / i;
            break;
        }
    }
    partes.push([1, '']);
    return partes;
}

Some examples would be:

Calcular(4); // "[[4,2],[2,2],[1,""]]" 
Calcular(3); // "[[3,3],[1,""]]" 
Calcular(5); // "[[5,5],[1,""]]"
Calcular(6); // "[[6,2],[3,3],[1,""]]"
Calcular(8); // "[[8,2],[4,2],[2,2],[1,""]]" 

After assembling the HTML depends on how you want the final format. Assuming we use the same table here is a suggestion:

jsfiddle: http://jsfiddle.net/uLmxnm0q/

Scroll to Top