Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Thomas Thomas
677 Points90% there on my Swift code, but now I'm stuck!
First and foremost, I want to let it be known that I am VERY new to Swift and coding in general. After completing several introductory swift courses I decided to attempt to write my own code from scratch.
I would like to create a code that based on a given date, (ie. month, day, year), will tell you the day of the week that it is. Obviously I know there is already a formula for this and I did my best to put this formula into code but I'm having difficulty piecing it all together.
If you have a few minutes would you mind letting me know what you would do?
Many Thanks!
Thomas
///////////////////////////////
//// Date to Week Day Code ////
///////////////////////////////
let monthCodes: [String:Int] = [
"Jan": 0,
"Feb": 3,
"Mar": 3,
"Apr": 6,
"May": 1,
"Jun": 4,
"Jul": 6,
"Aug": 2,
"Sep": 5,
"Oct": 0,
"Nov": 3,
"Dec": 5
]
let weekDayCodes = [
0: "Sunday",
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday"
]
// Let's say I use the Date: April 2, 1993
var day = 2
var century = 19
var year = 93
var centuryCode = 0
var monthCode = monthCodes["Apr"]
var month = monthCode
var yearCode = (year + (year/4)) % 7
var dayCode = (day % 7)
// Century Code //
switch century {
case 3, 7, 11, 15, 19: centuryCode = 0
case 4, 8, 12, 16, 20: centuryCode = 6
case 1, 5, 9, 13, 17: centuryCode = 4
case 2, 6, 10, 14, 18: centuryCode = 2
default: print("Please check birth year.")
}
// Results Code //
var codeTotal = (6 + dayCode + centuryCode + yearCode)
var answer = weekDayCodes[codeTotal % 7]
// Question //
So in the print statement below obviously there are 2 issues/bad code:
Instead of typing the word "April" in the string how can I replace it with a variable like I have done for (day), (century) etc?
Why is it printing the word 'Optional("Friday"), instead of just 'Friday'?
print("If you were born on April \(day), \(century)\(year), then you were born on a \(answer)!")
Thomas Thomas
677 PointsHey Jhoan,
The numbers that come after the months are not in order because they are not referring to the order of the months it's part of a formula to take into consideration the offset of days between each month overtime. See this link for the formula. But don't work too hard on this! I have kept going with my course and think I have found a much cleaner way to write this code! I will keep you posted, and thanks for your interest in helping me!
1 Answer
jonlunsford
16,746 PointsThomas:
From your code, you have hardcoded the values for day, century and year from the string date (i.e.; 2, 19, and 93). Also, I noticed that you have declared a variable named 'month' that is set to the monthcode value. I did not see where you used this variable anywhere else in your code. One option would be to hardcode 'April' into this value and output it in your print function same as the other variables.
The values in Dictionaries (key, value) are stored in swift using a special type of object called an Optional type. What this means is it can either contain a value or contain 'nil'. Your print statement is returning this optional type for the variable answer. You can downcast this type using the variable 'answer!' (the exclamation is actually an operator in this situation). For more information, see the section titled Downcasting in this article:
So if you change your print statement to the following it should work (or at least in my version of XCode it does).
print("If you were born on April \(day), \(century)\(year), then you were born on a \(answer!)!")
Jhoan Arango
14,575 PointsJhoan Arango
14,575 PointsHey Thomas : I have a question
Why are the months not in order ? Jan = 1 , Feb = 2 , Mar = 3 etc.
Help me understand your code a little.. If you can explain your code flow.
I like seeing people challenging themselves, so keep up the good work.
I will try to help you or guide you as much as I can.