Hi friends! It’s been a while since my last post as I’ve been dealing with medical issues related to my health. I might be a bit slow for the next couple of days due to the medications. But I’m still working through on my code projects and writing out my progress.
Here’s the CodePen where you can play around with the numbers if you want to try out different multiples and printing out number variations:
Breaking down the code
Keeping the format simple, I’m using Bulma CSS framework. Adding the
<span id=“output”>tag will allow us to print out the numbers and text on the page. I also adjusted the
bodysection to where the text will be centered and the font size of the
spanelement will be around 1.5rem.
varsince it’ll be within the block-scope of the function when the output occurs during the script. If you want to learn more about the differences between the variables, you can check out this article here.
Since we want to print out 24 numbers, we’ll be using the for loop to declare the following:
for (i = 0; i < 50; i++).
Within the for loop, we’ll be breaking down the if-else statements to determine how the text and numbers will be printed. The first portion of the code will determine if the number we are calculating will be a multiple of 24. For this to happen, we want to utilize the modulus operator for us to determine if the remainder will equal to zero. If it does, then it’ll provide the output “The Cow Goes Moo” on a separate line.
Second and third nested loops will follow the same format as the previous code stated, except this time it’ll determine if the multiples of 4 and 6 will be equivalent to zero. If it does, their equivalent outputs will be printed on separate lines.
The final else statement will simply print out the number if none of the conditions are met on the script.
Hope you enjoyed this post! If you happen to like it, feel free to share. You can also follow me on Twitter on my coding adventures.
I write daily web development tips that help you become a more efficient developer. 👨💻⚡️
Ah the good 'ol fizzbuzz challenge... To be honest, I always wonder what a company want's to see, the loop is the quickest and neatest solution, so in that case you did an amazing job!
Also, Diana, Please take care of yourself hope you get a full recovery soon! 🐮