How would I do this in a Javascript loop?

I have to write a program / function  that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five, I've got to print "Buzz". For numbers which are multiples of both three and five, I've got to  print "FizzBuzz".

How?
brucegustPHP DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Randy PooleCommented:
Use the modulus operator to see if your loop variable has a remainder
<html>
<head>
<script language="javascript">
	function doloop()
	{
		var c1,c2,r,o;
		o="";
		for (var c=1;c<=100;c++)
		{
			r="";
			c1=c % 3;
			c2=c % 5;
			r+=((c % 3)==0)?"Fizz":"";
			r+=((c % 5)==0)?"Buzz":"";
			r=r==""?(c):r;
			o+=r+"<br />";
		}
		document.getElementById("output").innerHTML=o;
	}
</script>
<head>
<body onload="doloop();">
	<div id="output">
	</div>
</body>
</html>

Open in new window

0
gplanaCommented:
Try this:

<html>
<body onload="doloop();">
<script language="javascript">
		var c1,c2,r,o;
		for (var i=1;i<=100;i++)
		{
			if (i%15==0) document.write('FizzBuzz<br />')
                        else if (i%5==0) document.write('Buzz<br />')
                        else if (i%3==0) document.wirte('Fizz<br />');
                        else document.write( i + '<br />');
		}
</script>

</body>
</html>

Open in new window


Hope it helps.
0
gplanaCommented:
Sorry, a couple of errors on my script. Here is the file with the errors solved.
prova2.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brucegustPHP DeveloperAuthor Commented:
Excellent!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.