Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading