sum the triangle

by trentHarlem

HTML

<h1>
  sum triangle
</h1>
<div id='output'>

</div>

<div id='output2'>
</div>

CSS

* {
  font-family: system-ui;
}
#output2 {
  position: absolute;
  top: 80px;
}

JavaScript

function sumOddNumbers(n) {
  let arr = []
  let row = []
  let count = 1
  let output = document.getElementById('output')
  let output2 = document.getElementById('output2')

  document.body.style.textAlign = 'center'
  //get the numbers
  for (let i = 0; i <= n; i++) {
    if (i % 2 == 1) {
      arr.push(i)
    }
  }
  console.log(arr.length, 'Odd #s between 0 and ', n, arr)

  // get the rows and sums
  for (let i = 0; i < arr.length; i++) {
    let row = arr.splice(0, count)
    let sum = row.reduce(add)
    output2.innerHTML += `${row.reduce(add)}<br>`;
    output.innerHTML += `${row.join(" ")}<br>`
    console.log(row)
    console.log(count)
    count++
    row = [];
  }

}
// function for .reduce
function add(total, num) {
  return total + num;
}

sumOddNumbers(99)