JSFiddle - React, Tailwind, and code Playground

by Stephen Hollingsworth

JavaScript

// can't have "var" in parameter!!! ???
function sumMults3or5(numberContainingMultiples)
{
  var index3 = 1;
  var index5 = 1;
  var multiplesTotal = 0;

  // TODO, make general to accommodate any 2 numbers? And then extend to any array of multiples...  
  while ( index3*3 < numberContainingMultiples )
  {
    var multiple = index3 * 3;
    multiplesTotal += multiple;
    index3++;
  }

  while ( index5*5 < numberContainingMultiples )
  {
    var multiple = index5 * 5;
    // Subtract if it is also a multiple of 3 (as I understand the solution, '15' should not be added twice)
    if (( multiple % 3 ) !== 0 )
    {
       multiplesTotal += multiple;
    }
    index5++;
  }

  alert(multiplesTotal);
};

sumMults3or5(1000);