简体   繁体   English

尝试使用var而不是数字简化javascript。 我要解决所有这些错误吗?

[英]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. 为了简化代码,我正在尝试使用模数按变量进行FizzBu​​zz计算。 They are supposed to count up to 140 displaying True or False. 他们应该最多计数140个显示True或False的内容。

My previous 'if' statement looks like this (and it worked) : 我之前的“ if”语句看起来像这样(并且有效):

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. 感谢您提供任何帮助,因为我仍然对JavaScript和编码尚不陌生。

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>";
    }
  }
}

EDIT---------------------------------------------------------------------------- 编辑 - - - - - - - - - - - - - - - - - - - - - - - - - ---------------------------

Alright, so now my only problem is that the numbers that should be divisible by five, are being displayed as divisible by three when clearly that isn't possible: 好了,所以现在我唯一的问题是,在显然不可能的情况下,应该被五分的数字显示为被三分的数:

    function clickAlert2() {

  function checkDivision(counter) {

    var firstDivisor = 3,
      secondDivisor = 5;

    return (counter % firstDivisor === 0) || (counter % secondDivisor === 0);
  };

  for (var iCounter = 1; iCounter <= 140; iCounter++) {

    if (checkDivision(iCounter)) {
      document.getElementById("ngList").innerHTML +=
        iCounter + ". True [3] <br>";
    } else if (checkDivision(iCounter)) {
      document.getElementById("ngList").innerHTML +=
        iCounter + ". True [5] <br>";
    } else {
      document.getElementById("ngList").innerHTML +=
        iCounter + ". False <br>";
    }
  }
}

Your checkDivision is a Boolean variable and not a function. 您的checkDivision是一个布尔变量,而不是一个函数。 So you simply pass it as the expression in the if and else if statements. 因此,您只需在if和else if语句中将其作为表达式传递即可。

if(checkDivision){.....}

Also, for a fizzbuzz you could make a function like this. 另外,对于嘶嘶声,您可以执行以下功能。

function fizzbuzz(num){
  if(num % 3 === 0 && num % 5 === 0)
     return "FizzBuzz";
  else if(num % 3 === 0)
     return "Fizz";
  else if(num % 5 === 0)
     return "Buzz";
  else return "none";
}

Now you can compare the string returned by this function to know exactly whether it is a fizz, or Buzz, or a fizzbuzz 现在,您可以比较此函数返回的字符串,以准确知道它是嘶嘶声还是Buzz还是fizzbuzz

I can realize from your code segment you are trying to detect the numbers between 1 - 140 which are divisible by 3( firstDivisor ) and 5( secondDivisor ). 我可以从您的代码段中意识到,您正在尝试检测1-140之间的数字,这些数字可以被3( firstDivisor )和5( secondDivisor )整除。 you need loop which go through 1 -140 and a function ( checkDivision ) function which determine given number is the number divisible by 3 and 5. 您需要通过1 -140的循环和确定给定数字为3和5可以整除的数字的函数( checkDivision )函数。

function clickAlert2() {

  function checkDivision(counter, divisor) {

    return (counter % divisor === 0);
  };

  var firstDivisor = 3,
    secondDivisor = 5;

  for (var iCounter = 1; iCounter <= 140; iCounter++) {

    //check divsible by both divisors
    if (checkDivision(iCounter, firstDivisor) && checkDivision(iCounter, )) {
      document.getElementById("ngList").innerHTML +=
        iCounter + ". True [" + firstDivisor + " " + secondDivisor + "] <br>";
    }

    //check divible by first divisor
    else if (checkDivision(iCounter, firstDivisor)) {
      document.getElementById("ngList").innerHTML +=
        iCounter + ". True [" + firstDivisor + "] <br>";
    }
    //check divible by second divisor
    else if (checkDivision(iCounter, secondDivisor)) {
      document.getElementById("ngList").innerHTML +=
        iCounter + ". True [" + secondDivisor + "] <br>";
    }
    //cannot divisible either divisors
    else {
      document.getElementById("ngList").innerHTML +=
        iCounter + ". False <br>";
    }
  }
}

Is this what you are looking for? 这是你想要的?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM