Post Images
"Front-end"

How to Write FizzBuzz in JavaScript!

FizzBuzz is a classic programming challenge that is commonly used in technical interviews to assess a candidate's programming skills. The challenge requires you to write a program that prints the numbers from 1 to 100, but for multiples of 3, print "Fizz" instead of the number, and for multiples of 5, print "Buzz". For numbers that are multiples of both 3 and 5, print "FizzBuzz".
 
In this blog post, we will explore how to write FizzBuzz using vanilla JavaScript.  
The code is structured in three functions with the second one doing the logic that makes FizzBuzz work.
getValues()

This function gets the value from the HTML page that the user has input and assigns them to the variables that first checks that they are in fact numbers, if a non-number is present it will present a Sweet Alert saying that only numbers are allowed, and if the ending number is greater than 5000 it will present a Sweet Alert that it will not accept numbers that big. Then these variables get passed to the logic function.

 

 

generateEliteCode()

This function is the logic function it receives the parameters from the getValues function that the user has input and first creates a table in the HTML doc. Then this function performs a for loop that will loop through each number until it gets to the end value, each time it increments by 1. Inside of this for loop it checks each number to see if it's divisable by the "EliteCode" variable, if it is it'll print "EliteCode", if not then it'll check if the number is divisable by the "Code" value, if so it'll print "Code." Lastly, it'll check if the number is divisable by the "Elite" value, if so it'll print "Elite" and if not it will print the number. This will loop through the entire array of numbers.

 

 
displayEliteCode()

This function is the display function it receives the parameter "arr" from the getValues function then displays the results in the HTML table.

 
 

Post a comment

0 comments