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 :
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>