簡體   English   中英

output 元素如何放入 javascript 中的數組中?

[英]How can the output elements put in an array in javascript?

function main() {
    var increase = parseInt(readLine(), 10);
    var prices = [98.99, 15.2, 20, 1026];
    //your code goes here
    for(var i=0;i<prices.length;i++)
    {
    var b=prices[i]+increase;
    console.log(+b);
   }
}

這里 output 顯示在單獨的元素中,但我希望所有元素都在一個數組中。

您需要將結果放入數組中,然后記錄 output。 檢查下面的代碼

var output = []
function main() {
    var increase = parseInt(readLine(), 10);
    var prices = [98.99, 15.2, 20, 1026];
    //your code goes here
    for(var i=0;i<prices.length;i++)
    {
    var b=prices[i]+increase;
    output.push(+b);
   }
}
main()
console.log(output)

您應該只創建數組並推入它:

 function main() { let result = []; var increase = parseInt(readLine(), 10); var prices = [98.99, 15.2, 20, 1026]; //your code goes here for(var i=0;i<prices.length;i++) { var b=prices[i]+increase; result.push(b); } return result; }

暫無
暫無

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

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