mercredi 26 septembre 2018

Trying to simplify javascript with var instead of numbers. Am I going about this all wrong?

I'm trying to make my FizzBuzz do calculations using modulus by variable in order to simplify the code. They are supposed to count up to 140 displaying True or False.

My previous 'if' statement looks like this (and it worked) :

if (i % 3 === 0 && i % 5 === 0)

The example that I've seen looks like this:

if (checkDivision(iCounter, secondDivisor))

I've created variables for the counter, the two divisors, and the modulus checker, but I can't seem to get it to work.

Any help is appreciated because I am still very new to javascript and coding in general.

Here is my code so far:

    function clickAlert2() {
  var firstDivisor = 3;
  var secondDivisor = 5;
  for (var iCounter = 1; iCounter <= 140; iCounter++) {
    var checkDivision =
      iCounter % firstDivisor === 0 || iCounter % secondDivisor === 0;
    if (checkDivision(iCounter, firstDivisor)) {
      document.getElementById("ngList").innerHTML +=
        checkDivision + ". True [3] <br>";
    } else if (checkDivision(iCounter, secondDivisor)) {
      document.getElementById("ngList").innerHTML +=
        checkDivision + ". True [5] <br>";
    } else {
      document.getElementById("ngList").innerHTML +=
        checkDivision + ". False <br>";
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire