Recursion 4

by Matthew Day

JavaScript

var subtotal = 0;

sum = (n) => {
	subtotal += n;
	if(n === 0) {
  	subtotal += n;
    console.log(`The sum of all integers is ${subtotal}.`)
  }
  else if(n > 0) {
  	console.log(`Adding ${n} to the subtotal.`)
    sum(n - 1);
  }
}

// Vary the number passed to sum() to see different results.
sum(4);


// SOURCE
// https://www.youtube.com/watch?v=t4MSwiqfLaY