[英]How can add elements to an array but put each element in a separate row in 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.