Trying to find factors of a number in JS

前端 未结 13 2822
陌清茗
陌清茗 2021-02-19 04:52

I am just starting JS, and understand the concept of finding a factor. However, this snippet of code is what I have so far. I have the str variable that outputs nothing but the

13条回答
  •  半阙折子戏
    2021-02-19 05:42

    function calculate(num) {
        var str = "0";
        for (var i = 1; i <= num; i++) {
            if (num % i == 0) {
                str += ',' + i;
            }
        }
        alert(str);
    }
    
    calculate(232);

    http://jsfiddle.net/67qmt/

提交回复
热议问题