如何将单个元素数组作为单个列/列表返回?

时间:2016-06-02 10:28:58

标签: javascript arrays

如果我运行下面的函数,它会返回如下内容:1,2,3,4 - 这是预期的,但是如何将单个列返回到这样的textarea字段?

1
2
3
4

function ReturnSingleColumnFromArray()
{   
    var randomNumberArray = [];
    var inputArrayLength = parseInt(document.getElementById("txtMultiples").value);
    var outputTextArea = document.getElementById("textarea");       
    var i = 0;  

    if (howMany < 101 && howMany > 0) {
        while (i < howMany) {
            randomNumberArray[i] = FunctionToReturnRandomNumber();  
            i++;
        }
        outputTextArea.innerHTML = randomNumberArray;
    } else {
        outputTextArea.innerHTML = "";
        alert("Value must be between 1 and 100");
    }       
}

1 个答案:

答案 0 :(得分:1)

<script>
    function ReturnSingleColumnFromArray() {
        var randomNumberArray = [];
        var howMany = parseInt(document.getElementById("txtMultiples").value);
        var outputTextArea = document.getElementById("textarea");
        var i = 0;

        if (howMany < 101 && howMany > 0) {
            while (i < howMany) {
                randomNumberArray[i] = FunctionToReturnRandomNumber();
                i++;
            }
            outputTextArea.innerHTML = randomNumberArray.join('\n');
        }
        else {
            outputTextArea.innerHTML = "";
            alert("Value must be between 1 and 100");
        }
    }

    function FunctionToReturnRandomNumber() {
        return Math.round(Math.random() * 100, 0);
    }

</script>