samedi 27 janvier 2018

Swift - using my variables in an "if" statement

// hi, I’m still really new to programming and I know that there’s something fundamental that I’m missing. I’m trying to use the if statement below (I commented it out). The code works perfectly except that when I “uncomment” it, I get an error. Please help me with this issue and if you have any advice on things you’d change, please let me know as I’d rather not make a bad habit of things that should be done differently. On that note, if you were born in the 1900’s and would like to try this code, please feel free! It’ll tell you the day of the week you were born on. Unless you were born in January or February (that’s why I need the if statement fixed). Also, if you know where and when to add enums for the arguments called in the function (the day, month and year), that would be awesome as well. Thanks in advance!

class Birthday{

    func findDayOfWeek(_ month:Int, _ day:Int, _ year:Int) -> String{

    var runningNumber = 0

    //print( "start with " , runningNumber)

    let shortenedYear = (year - 1900)

    let remainder = shortenedYear % 12

    runningNumber += remainder

    //print("add the remainder" , runningNumber)    

    var testNumber = runningNumber

    while testNumber > 3{
    runningNumber += 1
    testNumber -= 4
    }

    //print("because we added one for every four" ,runningNumber)

    let dividedByTwelve = ((shortenedYear - remainder) / 12)

    runningNumber += dividedByTwelve

    //print("add the amount of 12’s to get to your year" , runningNumber)

    var testNumber2 = runningNumber

    while testNumber2 > 7{
    runningNumber -= 7
    testNumber2 -= 7
    }
    //print("if the previous number was over 7, we looped down by 7 to get " , runningNumber)

    **//if remainder == 0{
    let doomsdayPerMonth: [Int:Int] = [ 1:31, 2:28, 3:7, 4:4, 5:9, 6:6, 7:11, 8:8, 9:5, 10:10, 11:7, 12:12]

    //}
    //else{
    //let doomsdayPerMonth: [Int:Int] = [ 1:25, 2:29, 3:7, 4:4, 5:9, 6:6, 7:11, 8:8, 9:5, 10:10, 11:7, 12:12]
    //}**
    let doomsdayOfMonth = doomsdayPerMonth[month]

    var daysUntilBDay = day - doomsdayOfMonth!

    while daysUntilBDay < 0{
    daysUntilBDay += 7
    }

    runningNumber += daysUntilBDay % 7

    //print("since the day of the month is \(day), looping down by seven would be \(daysUntilBDay % 7). Add that to above to get \(runningNumber).")

    runningNumber = runningNumber % 7

    //print(runningNumber)

    let days: [Int:String] = [0:"Wednesday", 1:"Thursday", 2:"Friday", 3:"Saturday", 4:"Sunday", 5:"Monday", 6:"Tuesday"]

    let months: [Int:String] = [1: "January", 2: "February", 3: "March", 4: "April", 5: "May", 6: "june", 7: "July", 8: "August", 9: "September", 10: "October", 11: "November", 12: "December"]

    let dayPicker = days[runningNumber]

    return ("\(months[month]!) \(day), \(year) was a \(dayPicker!).")  
    }

    func writeInWords(_ month:Int, _ day:Int, _ year:Int) -> String{

    let month1: [Int:String] = [1: "January", 2: "February", 3: "March", 4: "April", 5: "May", 6: "June", 7: "july", 8: "August", 9: "September", 10: "October", 11: "November", 12: "December"]

    let monthName = month1[month]
    return ("\(monthName!) \(day), \(year)")
    }
}

let personBday = Birthday()
print (personBday.findDayOfWeek(3,24,1914))
//print(personBday.writeInWords(4,27,1988))

Aucun commentaire:

Enregistrer un commentaire