JSFiddle - React, Tailwind, and code Playground

by Laurens Marcelis

HTML

<div class="count">

</div>
<div class="instructions">
</div>

JavaScript

let theArray = [[10,2],8,3,1];
let counter = document.querySelector(".count");

let instructions = document.querySelector(".instructions");
counter.innerHTML += arraySum(theArray);

function arraySum(arr) {
	//we start with an empty sum
  let sum = 0;
  
  for(let i = 0; i < arr.length; i++) {
    instructions.innerHTML += '<hr>checking if there are more arrays in here</br>';
    instructions.innerHTML += 'this is '+ Array.isArray(arr[i]) + '</br>';
    
    if(Array.isArray(arr[i])) {
    	
    	instructions.innerHTML += 'we go in the new array </br>';
      instructions.innerHTML += 'we call the function again to check if there is another array or we just add the number</br>';
      sum+=arraySum(arr[i]);
      
    } else if(typeof arr[i]==='number') {
    instructions.innerHTML += 'we just add the number '+arr[i] + ' to the sum</br>';
      sum+=arr[i];
    }
  }
  return sum;
}