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

Time:11-26

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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();
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related