Codecademy FizzBuzz app, stuck on step 1

丶灬走出姿态 提交于 2019-12-18 07:13:06

问题


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?


回答1:


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 ;)




回答2:


First, your loop is not even getting off the ground:

for ( i = 1; i > 20; i++ )

will not iterate even once since the middle condition is initially false. I think you meant:

for ( i = 1; i <= 20; i++ )

"FizzBuzz";

is just a string literal that JavaScript is ignoring. You need to output this string somehow:

console.log("FizzBuzz");

Also, this block

else {
    i;
  }

is also not doing anything. Did you want to display numbers that were divisible by neither 3 nor 5?

else {
    console.log(i);
  }

And, on a similar note, what is the "hello" at the top loop supposed to do?


On a more positive note, I see you're using strict equality:

if ( i % 5 === 0 )

this is a very, very good habit to be in. The non-strict equality operator == will do all sorts of implicit conversions if you're not careful. Always use strict equality unless you purposefully want these implicit conversions to happen.




回答3:


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



回答4:


for (var i = 1; i <= 20; i++) {
    if (i % 15 === 0) {
        console.log("FizzBuzz");
    }
    else if (i % 3 === 0) {
        console.log("Fizz");
    }
     else if (i % 5 === 0) {
        console.log("Buzz");
    }
    else{
    console.log(i);
    };
} 



回答5:


for ( i = 1; i > 20; i++ ) means that the program will do nothing. If you expect the variable i to begin with 1 and end with 20, you should do that like for( i = 1; i <= 20; i++). And if you want to test some number you want, you should use the function like:

function TestFizzBuzz(num){
    ...
    ...
}
TestFizzBuzz(1);
TestFizzBuzz(990);
...



回答6:


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



回答7:


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]);
    }
}



回答8:


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')));};



回答9:


  for(var i =1; i<=20; i++){
  if(i % 15 === 0){
      console.log("FizzBuzz");
  }
  else  if(i % 5 === 0){
      console.log("Buzz");
  }

    else  if(i % 3 === 0){
      console.log("Fizz");
  }
  else{
       console.log(i);
      }

    };



回答10:


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>


来源:https://stackoverflow.com/questions/8797834/codecademy-fizzbuzz-app-stuck-on-step-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!