簡體   English   中英

在 function 內包含數組元素的 For 循環。 如何退回 function output?

[英]For loop with array elements Inside function. How to return the function output?

我如何通過 function 調用獲得以下兩個 for 循環的 console.log 的 output 。 下面給出的代碼片段。

下面的代碼:

 var scoreDolphins = [96, 108, 89]; var scoreKolas = [88, 91, 110]; var avgD_Div = scoreDolphins.length; console.log(avgD_Div); var avgK_Div = scoreKolas.length; console.log(avgK_Div); var calcAvgF = function() { for (let scoreItem_D = 0; scoreItem_D < scoreDolphins.length; scoreItem_D++) { console.log(scoreDolphins[scoreItem_D]); } console.log("---------------------------------------------------------------------------"); for (let socreItem_K = 0; socreItem_K < scoreKolas.length; socreItem_K++) { console.log(scoreKolas[socreItem_K]); } return calcAvgF; } console.log(calcAvgF);

calcAvgF不應返回calcAvgF itemslf。 你也應該調用calcAvgF function 而不是console.log(calcAvgF)

 var scoreDolphins = [96, 108, 89]; var scoreKolas = [88, 91, 110]; var avgD_Div = scoreDolphins.length; var avgK_Div = scoreKolas.length; var calcAvgF = function () { for (let scoreItem_D = 0; scoreItem_D < scoreDolphins.length; scoreItem_D++) { console.log(scoreDolphins[scoreItem_D]); } for (let socreItem_K = 0; socreItem_K < scoreKolas.length; socreItem_K++) { console.log(scoreKolas[socreItem_K]); } } calcAvgF();

暫無
暫無

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

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