Question

In: Computer Science

Consider the series of numbers beginning at start and running up through end inclusive. For example...

Consider the series of numbers beginning at start and running up through end inclusive. For example start=4 and end=8 gives the series 4, 5, 6, 7, 8. Write a complete JavaScript function named fizzBuzz (including a function heading) that returns a new array containing the number, except for multiples of 3, use "Fizz" instead of the number. For multiples of 5 use "Buzz". For multiples of both 3 and 5 use "FizzBuzz".

The following code must print the output that follows the code:

array = fizzBuzz(8, 16);
for(var i = 0; i < array.length; i++) {
   console.log(array [i])
}

Output:
8

Fizz

Buzz

11

Fizz

13

14

FizzBuzz

16

Solutions

Expert Solution

HTML & Javascript:

<!DOCTYPE html>
<html>
  
   <head>
       <script type="text/javascript">
          
           //Function that runs when page is loaded
           function run() {
               //Calling function
               array = fizzBuzz(8, 16);
              
               //Iterating over array
               for(var i = 0; i < array.length; i++) {
                   //Printing results
                   console.log(array [i]);
               }
           }
          
           //Function definition
           function fizzBuzz(start, end) {
               //Array declaration
               var fizzB = [];
              
               //Iterating over range
               for(var i = start; i <= end; i++) {
                   //Checking for multiples of 3 & 5
                   if(i%3==0 && i%5==0) {
                       //Adding word FizzBuzz
                       fizzB.push("FizzBuzz");
                   }
                   //Checking for multiples of 3
                   else if(i%3==0) {
                       //Adding word Fizz
                       fizzB.push("Fizz");
                   }
                   //Checking for multiples of 5
                   else if(i%5==0) {
                       //Adding word Fizz
                       fizzB.push("Buzz");
                   }
                   //Pushing number
                   else {
                       fizzB.push(i);  
                   }
               }
               return fizzB;
           }

       </script>
   </head>
  
   <body onload="run()">
   </body>
  
</html>

______________________________________________________________________________________

Sample Run:


Related Solutions

In Lotto649, a player selects 6 numbers with replacement from the integers 1 through 49, inclusive....
In Lotto649, a player selects 6 numbers with replacement from the integers 1 through 49, inclusive. In the actual game, the order of the selection of numbers does not matter. In other words, if the winning lottery number for the week is (2, 17, 29, 8, 10, 3), then any permutations of this set of 6 numbers will also be considered a ‘winning ticket’. a) How many unique lottery numbers are there? Include point-form explanations of you are counting this....
a.) A series of 25 end-of-year deposits beginning with $1390 at the end of year 1...
a.) A series of 25 end-of-year deposits beginning with $1390 at the end of year 1 and increasing by $410 per year with a 8% interest rate. What Uniform Series of deposits would result in the same cumulative balance? b.) A series of 30 end-of-year deposits are made into an account that returns 4.2.% per year compounded annually. The first deposit is $5,251 and the deposits increase by 4.2% each year. How much will be in the account immediately following...
Solve please in python b) Create a program that shows a series of numbers that start...
Solve please in python b) Create a program that shows a series of numbers that start at a and increase from 5 to 5 until reaching b, where a and b are two numbers captured by the user and assumes that a is always less than b. Note that a and b are not necessarily multiples of 5, and that you must display all numbers that are less than or equal to b. c) Create a program that displays n...
B. A series of 25 end-of-year deposits beginning with $1135 at the end of year 1...
B. A series of 25 end-of-year deposits beginning with $1135 at the end of year 1 and increasing by $260 per year with a 6% interest rate. What Uniform Series of deposits would result in the same cumulative balance? $ C. John makes 35 end-of-year deposits into a retirement account that returns 5.0.% per year compounded annually. His first deposit is $7500 and he increases the deposits by 8.9% each year. How much will be in the account immediately following...
how does the heart, arteries and veins start and where they end up?
how does the heart, arteries and veins start and where they end up?
In Excel: How do I turn a series of dates into a sequence of numbers (example...
In Excel: How do I turn a series of dates into a sequence of numbers (example 1,2, 3)?
In setting up expatriates for success from beginning to end of an international assignment, there are...
In setting up expatriates for success from beginning to end of an international assignment, there are four key considerations that managers and HR professionals must consider. Describe each (recall)
You are the Finance Director of a start-up that delivers online ads to end users and...
You are the Finance Director of a start-up that delivers online ads to end users and you are about to approve the budget for the next fiscal year. You notice that your team has projected the annual revenue goal through equal increase of monthly ad deliveries, which assumes least number of deliveries in January and the most number of deliveries in December. Before approving the budget, you want to make sure that there is indeed such a difference between the...
Problem 1. A start-up company called “COMM387” is raising it’s Series A round at a $12M...
Problem 1. A start-up company called “COMM387” is raising it’s Series A round at a $12M pre-money valuation and a $10 price per share. The amount raised in the Series A round is $1M. Two years before the Series A round, the company raised its seed round by issuing a convertible note with a $3M valuation cap and a 20% discount. The principle amount of the convertible note is $1M, with an annual non-cumulative interest rate of 5%. 1. What...
Pharoah Inc., management is expecting a new project to start paying off, beginning at the end...
Pharoah Inc., management is expecting a new project to start paying off, beginning at the end of next year. Cash flows are expected to be as follows: 0 1 2 3 4 5 $436,676 $474,452 $463,455 $465,326 $537,444 If Pharoah can reinvest these cash flows to earn a return of 7.2 percent, what is the future value of this cash flow stream at the end of 5 years? What is its present value?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT