*/ write a function daysPastThisYear that takes the current date as the name of the current month and the current day, e.g., daysPastThisYear(month: "May", day: 12), and returns how many days have past since the beginning of the year. Use a "while" loop. Ignore leap years. You may user the following list and dictionary defined: let monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] let monthDays = ["January": 31, "February": 29, "March": 31, "April": 30, "May": 31, "June": 30, "July": 31, "August": 31, "September": 30, "October": 31, "November": 30, "December": 31]
E.g.
print(daysPastThisYear(month: "January", day: 12))
// prints 12
*/
//My code so far:
func daysPastThisYear(monthNames: [String], monthDays: [String:Int]) -> Int {
let monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
let monthDays = ["January": 31, "February": 29, "March": 31, "April": 30, "May": 31, "June": 30, "July": 31, "August": 31, "September": 30, "October": 31, "November": 30, "December": 31]
var i = 0
while month != monthNames[i] {
i
}
totalDays = 0
while i > 0 {
totalDays = totalDays monthDays[i - 1]
-= i
totalDays = totalDays day
print(totalDays)
}
}
print(daysPastThisYear(month: "January", day: 12))
Im an new to coding
Tried to calculate the number of accumulated days implementing two while loops
Multiple errors when attemtong to run
CodePudding user response:
Building on top of Leo Dabus's solution but using a while loop as requested
let monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
let monthDays = ["January": 31, "February": 29, "March": 31, "April": 30, "May": 31, "June": 30, "July": 31, "August": 31, "September": 30, "October": 31, "November": 30, "December": 31]
func daysPastThisYear(month: String, day: Int) -> Int {
var total = 0
var monthIndex = 0
// while loop though the monthNames array
while monthIndex <= (monthNames.count - 1) {
let monthName = monthNames[monthIndex]
if month == monthName {
total = day
break
} else {
total = monthDays[monthName] ?? 0
}
monthIndex = 1
}
return total
}
CodePudding user response:
You can iterate the month names and each iteration just check if the month name is equal to the month, if not true add the number of days of the month to the total otherwise add the day to the total and break the loop. After the loop just return the total. Something like:
func daysPastThisYear(month: String, day: Int) -> Int {
var total = 0
for monthName in monthNames {
if month == monthName {
total = day
break
} else {
total = monthDays[monthName] ?? 0
}
}
return total
}
edit/update:
Using a while loop
func daysPastThisYear(month: String, day: Int) -> Int {
var total = 0
var index = 0
while monthNames.indices.contains(index) && month != monthNames[index] {
total = monthDays[monthNames[index]] ?? 0
index = 1
}
return total day
}