javascript如何计算fizz,buzz和fiszbuzz的数量

时间:2015-07-17 16:10:01

标签: javascript html fizzbuzz

如何计算嘶嘶声,嗡嗡声和fizzbuzz的数量?我不知道怎么做!!



<!DOCTYPE HTML>
<html>
	<head>
		<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
		<script>
		var FizsBuzzOutput = ''
		var FizzCount = (i++)
		var BuzzCount = 
		var FizsBuzzCount = 

		//Loop through numbers from 1 tp 100
		//Write them oput to the screen

		for ( var i=1; i < 100; i++ ) {

			// document.write("<br />");

			//If the number is divisable by 3 then instead of writing the number, write "Fizz"

			// $( "body" ).append(  );
			

			if (i % 15 == 0){
				// document.write("FizsBuzz, ")
				FizsBuzzOutput += "FizsBuzz, "
			}

			else if ( i % 3 == 0 ){
				//document.write("Fizz, ")
				FizsBuzzOutput += "Fizz, "
			}

			
			else if (i % 5 == 0){
				//document.write("Buzz, ")
				FizsBuzzOutput += "Buzz, "
			}

			

			else {
				//document.write(i + ', ')
				FizsBuzzOutput += i + ', '
			}

		}

		document.write( FizsBuzzOutput )

		</script>

	</head>
	<body> </body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您没有正确声明变量。 你应该使用分号。这个应该是一个有趣的读物:Do you recommend using semicolons after every statement in JavaScript?

如果你开始使用/学习javascript eloquent javascript可能会对你有所帮助:http://eloquentjavascript.net/

这样的事情应该有效:

    var FizsBuzzOutput = '';
    var fizz = 0;
    var buzz = 0;
    var fizzbuzz=0;



    for ( var i=1; i < 100; i++ ) {


        if (i % 15 == 0){
            fizzbuzz+=1;                
            FizsBuzzOutput += "FizsBuzz, ";
        }

        else if ( i % 3 == 0 ){
            fizz+=1;
            FizsBuzzOutput += "Fizz, ";
        }


        else if (i % 5 == 0){
            buzz+=1;
            FizsBuzzOutput += "Buzz, "
        }       
        else {

            FizsBuzzOutput += i + ', ';
        }
        console.log(FizsBuzzOutput);
    }
 console.log('fizz count= '+ fizz);
 console.log('buzz count= '+ buzz);
 console.log('fizzbuzz count= '+ fizzbuzz);

这是一个jsFiddle:https://jsfiddle.net/e7caLpjd/1/