Codecademy FizzBuzz app, stuck on step 1

2019-01-27 01:13发布

Here's my code for Codecamedy's FizzBuzz lesson

var i;
for ( i = 1; i > 20; i++ ) {
  "hello"
  if ( i % 3 === 0 ) {
    if ( i % 5 === 0 ) {
      "FizzBuzz";
    }
    else {
      "Fizz";
    }
  }
  else if ( i % 5 === 0 ) {
    "Buzz";
  }
 else {
    i;
  }
}

I'm trying to first test whether or not the number (i) is divisible by 3. If it is, I then want to check whether it is also divisible by 5. If both conditions are true, I want it to say "FizzBuzz". If only the first condition is true, it should say "Fizz". Then, after determining that i is not divisible by 3, it should check whether i is divisible by 5 and show "Buzz" if that's the case. Failing all divisibility, it should just show the number.

As I expected... it doesn't work as expected. What terribly embarrassing mistakes have I made?

10条回答
欢心
2楼-- · 2019-01-27 01:16

Just for the sake of shortness, let's do it in one line:

for (i=1;i<21;i++){console.log(i+": "+(i%3?(i%5?i:'Buzz'):(i%5?'Fizz':'FizzBuzz')));};
查看更多
淡お忘
3楼-- · 2019-01-27 01:19

The goal is to print "Fizz" for numbers that are integral divisible by 3 (without a rest), "Buzz" by 5 and "FizzBuzz" by 3 and 5, else it should print the number.

  • modulo (%) returns the rest of a division, so if (x % y) returns 0 the checked division is integral
  • as modulo can return 0, we need to remember truthy and falsy values - 0 is a falsy value, therefore we need to negate the test if we want to check if a number is "truly" 0

    e.g.: !(3 % 3) => !(0) => !false => true

for (var i = 1; i <= 20; ++i) {
  if (!(i % 3) && !(i % 5)) { // check if "i" is integral divisible by 3 & 5
    console.log("FizzBuzz");
  } else if (!(i % 3)) {      // else check if "i" is only integral divisible by 3
    console.log("Fizz");
  } else if (!(i % 5)) {      // else check if "i" is only integral divisible by 5
    console.log("Buzz");
  } else {
    console.log(i);           // else print the number
  }
}
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

查看更多
叛逆
4楼-- · 2019-01-27 01:21

Your specific problems are that you have the wrong sense for the for loop and that a statement like "somestring" or i doesn't actually do anything. What you want to do is output it do the console (or other output stream of some sort) - how to do this depends on the environment your Javascript is running in, and where you want the information to go.

You can also keep in mind that any number evenly divisible by both three and five is a multiple of 15.

So you can simplify your code with something like:

for all numbers in range:
    if num is a multiple of 15:
        print "FizzBuzz"
        continue for loop

    if num is a multiple of 3:
        print "Fizz"
        continue for loop

    if num is a multiple of 5:
        print "Buzz"
        continue for loop

    print i

There are those that will complain about multiple exit or restart points in a loop but you can safely ignore them since they don't understand the reasons behind that guideline, to avoid spaghetti code.

Any code where you can see all the control flow on a single page (such as the eleven lines above) is incapable of being spaghetti code, especially given the consistent handling.


Here's the equivalent Javascript code, packaged into a web page for testing:

<html><head></head><body><script type="text/javascript">
    var i;
    for (i = 1; i <= 20; i++) {
        if (i % 15 === 0) {
            document.write ("FizzBuzz<br>");
            continue;
        };  

        if (i % 3 === 0) {
            document.write ("Fizz<br>");
            continue;
        };  

        if (i % 5 === 0) {
            document.write ("Buzz<br>");
            continue;
        };  

        document.write (i + "<br>");
    }   
</script></body></html>

which outputs, as desired:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
查看更多
别忘想泡老子
5楼-- · 2019-01-27 01:21

My solution:

var nums = new Array();

for (var i = 0; i < 20; i++){
    nums[i] = i + 1;
}

for (var i = 0; i < 20; i++){
    if((nums[i] % 5 == 0) && (nums[i] % 3 == 0)){
        console.log("FizzBuzz");
    }else if(nums[i] % 5 == 0){
        console.log("Buzz");
    }else if (nums[i] % 3 == 0){
        console.log("Fizz");
    }else{
        console.log(nums[i]);
    }
}
查看更多
女痞
6楼-- · 2019-01-27 01:28

After considering all the other very good answers here:

Since you're "stuck on step 1" with the code you've provided, I assume you did the same mistake I did after clicking your link and reading the instructions. Step 1 doesn't actually ask you to solve the Fizzbuzz problem. To pass this step, you only have to do something much simpler. Read the (not very good) instructions again ;)

查看更多
戒情不戒烟
7楼-- · 2019-01-27 01:29

What if we make things a bit more difficult? 1) No division or modulo operations allowed; 2) The loop must skip all unnecessary iterations. Here is the answer:

int n3 = 3;
int n5 = 5;
int i = 3;
while (i <= 100)
{
    Console.Write(i.ToString() + " - ");

    if (i == n3)
    {
        Console.Write("fizz");

        n3 = n3 + 3;
    }

    if (i == n5)
    {
        Console.Write("buzz");

        n5 = n5 + 5;
    }

    Console.WriteLine();

    i = n3 < n5 ? n3 : n5;
}
查看更多
登录 后发表回答