簡體   English   中英

打印本地范圍之外的函數結果

[英]Printing results of function outside of the local scope

我想知道如何在函數范圍之外打印函數的返回值。 我在網上進行了研究,發現下面的帖子只是給了我 arr 未定義。 有人可以指出我正確的方向嗎? 控制台日志未從函數打印變量

function multiplyAll(arr) {
    var product = 1;
    for (var i = 0; i < arr.length; i++) {
        for (var j = 0; j < arr[i].length; j++)
            product = product * arr[i][j];
    }
    //why is the product undefined?
    return product;
}
//modify values below to test code
multiplyAll([
    [60, 60],
    [24, 365]
]);

//console.log("Print results of product" + product);

你應該將它分配給一個變量,而不是自己調用multiplyAll ,如下所示:

var product = multiplyAll([[60,60],[24,365]]);

如果您只想打印結果,您可以做的另一件事是將函數調用放入console.log調用中,如下所示:

console.log("Print results of product" + multiplyAll([[60,60],[24,365]]));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM