Closed martyav closed 4 years ago
I just submitted my first draft
Awesome! I just got around to taking a look.
So far so good! I took a quick look and added in some headings. I recommend breaking up your main section into subheadings if possible.
Also, it may be a good idea to display chunks of your code as you go. For instance, you reference the various lines of code throughout your explanation, Why not copy that line out into it's own code block to further illustrate your point? I've added a sample under your line 2 section.
Of course, it's up to you how you want to approach the article. I just ask that you keep a similar structure. 👍 Nice work, by the way! You really gave a lot of detail which I think is great. Also, you add a lot of personality to your writing which I think will allow others to connect with your work.
Thank you. October has been very busy. I'll try to finish by this week
No worries! I haven't touched my own website in awhile other than to check stats... so, I feel you!
Updated with headings, and the two subsections on running the code, and renegade coder's project. filled out.
The Readability score improved a lot with just those few changes.
Hey @martyav! We've moved articles to GitHub pages. Here's a copy of yours if you want to transition it to the new site:
We return to the Swift programming language with that old chestnut, fizz buzz. According to urban legend, fizz buzz is frequently used on job interviews to test if a candidate has any clue as to how to code.
I've never seen it, personally, but maybe it has happened once or twice.
To review: fizz buzz tests basic mastery of programming concepts such as conditionals and loops. It involves printing out the word "Fizz" if a number is divisible by 3, the word "Buzz" if a number is divisible by 5, and the number itself if it not divisible by either. If the number is divisible by both 3 and 5, we print the word "FizzBuzz". To solve fizz buzz, we are usually tasked with creating a function to go over a range of numbers, printing the appropriate responses along the way.
The result should look like this:
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz
So, without any further adieu, let's get into the code!
func fizzBuzz(start: Int = 1, end: Int = 100) -> Void {
let range = start...end
for number in range {
guard number % 5 == 0 || number % 3 == 0 else {
print(number)
continue
}
var fizzAndOrBuzz = ""
if number % 3 == 0 {
fizzAndOrBuzz = "Fizz"
}
if number % 5 == 0 {
fizzAndOrBuzz += "Buzz"
}
print(fizzAndOrBuzz)
}
}
fizzBuzz();
Here we see a rather long and punctuation-filled function signature. Swift functions use named parameters, and the type of each parameter must be noted. This is a safety feature, ensuring that we don't pass in a float when we need an integer. So, in line 1, we see start: Int and end: Int, meaning that our function takes in two integer values, named "start" and "end".
You might think that the equal signs next to each parameter look like assignments, and you would be (sort of) correct. When writing a Swift function's signature, the equals sign is used to define a default value. If later, the user chooses to call the function without passing in any arguments, the compiler will be smart enough to look at the defaults, and run the function using those values instead. Swift functions don't need to have default arguments, but they can be nice to have if you know that you'll be frequently using certain values. They can also save you some headaches when building apps.
The little arrow after the argument names indicates the return type, which in this case is Void. Void just means our function doesn't return anything. Our function is only printing out words and numbers, so it isn't returning any values.
On line 2, we use the keyword let to define a new range of numbers:
let range = start...end
In Swift, values defined with let are immutable, and we are encouraged to use them liberally. This keeps us from accidentally changing something when we don't want or need to.
Ranges are their own type in Swift, and allow us to go over a series of numbers without setting up a list or array. Although we could have explicitly named the type here, the Swift compiler is able to infer that we want a range because we've given it two integer values, separated by an ellipsis.
We create a for-loop to transverse the range on line 4. Early versions of Swift allowed for C-style loops, but they've since been replaced with this less wordy syntax.
Line 5 presents a guard statement, a kind of conditional that only fires if a condition has not been met. Guard statements are frequently used in Swift to handle edge cases that might otherwise cause errors, and to avoid nested conditionals. Here, we use a guard statement to swiftly print any numbers that are not divisible by 3 or 5. Then, we continue on to the next number.
However, if a number is divisible by 3 or 5, we set up the variable fizzAndOrBuzz . This value is defined with the keyword var, which allows us to change it over the course of our function.
We use two if-statements to test for Fizziness and/or Buzziness. Both statements use the modulo operator, which gives us the remainder from any division between one integer and another. If one number is evenly divisible by another, the remainder is always 0.
Line 14 uses +=
to add "Buzz" on to the end of our variable, so that if our number is evenly divisible by both 3 and 5, we get the word "FizzBuzz", and not just "Fizz" or just "Buzz".
Finally, we print whatever is inside fizzAndOrBuzz at the end of our loop.
We exit the loop, we end the function, and we call fizzBuzz at the very bottom. Since we've defined some defaults, the script knows to go over the numbers 1 through 100 without having to be told to. We could also pass in arguments for the start and end of our range, if we like.
Swift is often run via XCode, Apple's IDE, or by installing Swift on Linux. However, XCode in particular is a heavy download, so unless you already have it installed, or are seriously contemplating iOS development, you probably should use this handy online Swift compiler.
Please be aware that Swift updates very frequently, and many online compilers use earlier versions of the language, which may present with incompatibilities and errors when trying to run this code. This code was written in Swift4.
Thanks! What do I need to do to get it into the publication pipeline for the new site?
You can pretty much copy this into a document titled 2018-11-24-swift.md
and place it at the following path: docs/projects/fizz-buzz/_posts/
.
You'll need to add some front matter like this to the top of the file:
---
title: Fizz Buzz in Java
layout: default
last-modified: 2018-11-06
featured-image:
tags: [java, fizz-buzz]
authors:
- stuin
---
All done! Thanks again for taking care of this. I appreciate it.
Added code snippet but need to add article as well