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

Unable to pass an array as function argument in Javascript

I am trying to pass an array as a function argument. When I run the HTML the browser displays a blank page. Please help:) (Obviously a coding noob)

<html>

<head>
  <script>
    var list = [7, 8, 9, 4];

    document.write(list);

    pass_array(array) {
      let sum = 0;
      for (let i = 0; i < list.length; i++) {
        sum = +list[i];
      }
      document.write(sum);
    }

    pass_array(this, list);
  </script>
</head>

</html>

>Solution :

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

You should replace pass_array(array) with function pass_array(array) (as for function declaration), and then use array values inside pass_array function instead

pass_array(this, list); should have only 1 param list like pass_array(list);. In this context, this is not referred to your list data

<html>

<head>
  <script>
    var list = [7, 8, 9, 4];

    document.write(list);

    function pass_array(array) {
      let sum = 0;
      for (let i = 0; i < array.length; i++) {
        sum += array[i];
      }
      document.write(sum);
    }

    pass_array(list);
  </script>
</head>

</html>
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