In: Computer Science
JavaScript
1. FizzBuzz Submit js file with functioning code Write a program that uses console.log to print all the numbers from 1 to 120, with two exceptions. For numbers divisible by 4, print "Fizz" instead of the number, and for numbers divisible by 10 (and not 4), print "Buzz" instead. When you have that working, modify your program to print "FizzBuzz", for numbers that are divisible by both 4 and 10 (and still print "Fizz" or "Buzz" for numbers divisible by only one of those).
Tip: numbers is clearly a looping job remainder (%) operator for checking whether a number is divisible by another number (has a remainder of zero) there are three possible outcomes for every number, so you’ll have to create an if/else if/else chain.
JavaScript wrote as below:
<h3>Program to print 1-120 number </h3>
<script>
for(var i = 1; i < =120; i += 1) {
if(((i%4)==0) &&((i%10)!=0)){
console.log("\nFizz");
//document.write("\nFizz");
}else if(((i%4)!=0) &&((i%10)==0)){
console.log("\nBuzz");
//document.write("\nBuzz");
}else if(((i%4)==0) &&((i%10)==0)){
console.log("\nFizzBuzz");
//document.write("\nFizzBuzz");
}else{
console.log("\n"+i);
//document.write("\n"+i);
}
}
</script>
Output:
Program to print 1-120 number
1 2 3 Fizz 5 6 7 Fizz 9 Buzz 11 Fizz 13 14 15 Fizz 17 18 19 FizzBuzz 21 22 23 Fizz 25 26 27 Fizz 29 Buzz 31 Fizz 33 34 35 Fizz 37 38 39 FizzBuzz 41 42 43 Fizz 45 46 47 Fizz 49 Buzz 51 Fizz 53 54 55 Fizz 57 58 59 FizzBuzz 61 62 63 Fizz 65 66 67 Fizz 69 Buzz 71 Fizz 73 74 75 Fizz 77 78 79 FizzBuzz 81 82 83 Fizz 85 86 87 Fizz 89 Buzz 91 Fizz 93 94 95 Fizz 97 98 99 FizzBuzz 101 102 103 Fizz 105 106 107 Fizz 109 Buzz 111 Fizz 113 114 115 Fizz 117 118 119 FizzBuzz