简体   繁体   中英

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

How can i get the output of these below two for loop's console.log via function call. Code snippet given below.

CODE BELOW:

 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 should not return calcAvgF iteslf. Also you should call calcAvgF function unstead of 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM