1

I'm not quite sure how to word this question, but perhaps an example will help...

Is there an operator which will show the placement of, say, 25, on a number range from 1-7?

For example:

25/7 returns 4

21/7 returns 7

22/7 returns 1

4/7 returns 4

etc.

Example of code:

var dayOfMonth: Int = 28

var aNumber: Int

aNumber = (dayOfMonth ) % 7

func dayOfTheWeek(day: Int) {

    switch day {
  case 0:
        print("Monday")
  case 1:
        print("Tuesday")
  case 2:
        print("Wednesday")
  case 3:
        print("Thursday")
  case 4:
        print("Friday")
  case 5:
        print("Saturday")
  case 6:
        print("Sunday")
    default:
        print("Error")
  }

}


dayOfTheWeek(day: aNumber)
  • Please first try yourself and share codes, so you can get better responses. – Kamal Panhwar Mar 02 '20 at 01:44
  • What is your goal? You don't need to manually enumerate the weekdays and the result depends on which month and year you are trying to figure out. You can use Calendar weekdaySymbols `Calendar.current.weekdaySymbols[weekday-1] ` https://stackoverflow.com/a/49042360/2303865 – Leo Dabus Mar 02 '20 at 02:14

1 Answers1

2

I think you're looking for the modulo operator, spelt % in Swift. With the difference that 21 % 7 is 0, not 7.

m % n could be thought of as "the remainder after dividing m by n"

Alexander
  • 59,041
  • 12
  • 98
  • 151
  • Ahh, I was so close. I was actually using modulo, but I couldn't quite get it there on my own. What I’m trying to do is input the day of the month, and have the code figure out the day of the week. It was aways off by 1 if I used modulo (for example I needed 1 to correspond to “Monday” in my switch statement, but it was always remainder 0) Realized I could decrease all the switch case statements by 1 to solve that problem though. Sorry, for the simplicity, but thanks a lot for the response! – Z400Racer37 Mar 02 '20 at 01:49
  • @Z400Racer37 You should mention this kind of context and past-attempts, otherwise we're just shooting at the dark. You'll get more responses that way, and they'll be better and more relevant to what you're actually doing. – Alexander Mar 02 '20 at 01:58
  • Agreed. Sorry, I was so busy trying to figure out how to word the question, I totally forgot about the context. I also posted the actual code for others to check out if they're interested. Thanks again. – Z400Racer37 Mar 02 '20 at 02:05
  • 1
    @Z400Racer37 Also, why are you trying to hand roll this? Why don't you just use `DateFormatter` (if you want the name, like "sunday") or `DateComponents` (if you want the number of the day within the week, for some other kind of computation)? https://gist.github.com/amomchilov/5bdd9a12c9db4ab9cfbb1eebc95119f1 – Alexander Mar 02 '20 at 02:05
  • 2
    @Z400Racer37 You can shift the output of the modulo operator by subtracting a value from the dividend and adding it back to the remainder; e.g. `(day - 1) % 7 + 1` yields a number in the range `[1, 7]` instead of `[0, 6]`. This is a good "trick" to know for situations like this even though in your case it's not really necessary. – trent Mar 02 '20 at 02:08
  • @Alexander-ReinstateMonica Currently, I'm playing around with switch statements as such -- I'm very new to coding and I'm trying to get down the basic logic, structures and syntax etc. Thanks for that code snippet -- I'm playing with it now, and I'll look into that method. – Z400Racer37 Mar 02 '20 at 02:39
  • @trentcl Yeah I actually did that initially, but figured it was cleaner to reformat the cases? Thanks. – Z400Racer37 Mar 02 '20 at 02:43
  • 1
    @Z400Racer37 In your case, yeah, just renumbering the cases would be better. Although this is a good opportunity to learn about the various formatters that come with foundation and such. `DateFormatter` is just one of them. It lets you print all the various parts of a date (years, months, days, week days, hours, minutes) in many different combinations (short form, long form, spelt out, as numbers, as numbers padded with 0s, etc.). It's really flexible. But most importantly, is that it defaults to "doing what's write", which is critical for non-English users. – Alexander Mar 02 '20 at 13:05