Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Last active March 26, 2024 21:17
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save TheMuellenator/ceed75150b5f7127c8bf38b3564e7ca3 to your computer and use it in GitHub Desktop.
iOS repl.it - Switch Challenge Solution
////Don't change this
var aNumber = Int(readLine()!)!
func dayOfTheWeek(day: Int) {
//Write your code inside this function.
switch day {
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case 4:
print("Thursday")
case 5:
print("Friday")
case 6:
print("Saturday")
case 7:
print("Sunday")
default:
print("Error")
}
}
//Don't change this
dayOfTheWeek(day: aNumber)
@straumandi
Copy link

straumandi commented Mar 10, 2020

why the below code just couldn't work?

////Don't change this
var aNumber =  Int(readLine()!)! 

func dayOfTheWeek(day: Int) {
  
  //Write your code inside this function.
  switch aNumber {
    case aNumber == 1 :
     print("Monday")
    case aNumber == 2 :
     print("Tuesday")
    case aNumber == 3 :
     print("Wednesday")
    case aNumber == 4 :
     print("Thursday")
    case aNumber == 5 :
     print("Friday")
    case aNumber == 6 :
     print("Saturday")
    case aNumber == 7 :
     print("Sunday")
    default :
     print("Error")
  }
  
  
}

//Don't change this
dayOfTheWeek(day: aNumber)

Your Code does not work because:

1.) You are trying to use an input parameter that is defined in repl.it in the console input. In repl.it the ' Int(readLine) ' keyword, tells you to put in a value for the parameter, in this case it is an Integer. To give the readLine a value, you'll have to click on the symbol at the upper right corner that looks like this:
Bildschirmfoto 2020-03-10 um 12 13 17

2.) You are using for both, the switch and the case the same argument, this will definitely not work because it does not make any logical sense
You will have to use the input parameter of the function that you are calling at the bottom, which is of course "day". I'll explain you why, so that you can fully understand it.

You have your function 'dayOfTheWeek' with an input parameter called 'day' that takes an Integer, right?
So, now we want to say that when the value of 'day' is equal to '1', we want to print "Monday", right?

Now, the moment of truth:

When you write down the keyword 'switch' , it does not literally mean switch this thing.
It means:
for the thing you write after the switch keyword, in that case 'day' , you want do define some different 'cases', here it is the value for the day, and when the value of ' day ' is equal to ' 1 ' then it executes the code after ' case 1: '
so now we want to say: when the value of 'day' is 1 --> then print "Monday"

So that's why the could for that would look like this:

`////Don't change this
var aNumber =  Int(readLine()!)! 

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

//Don't change this
dayOfTheWeek(day: aNumber)`

Hope that i could help you with that one, don't suffer, programming is not easy at all.
Takes time, be patient :)

@badmunkey
Copy link

why the below code just couldn't work?

////Don't change this
var aNumber =  Int(readLine()!)! 

func dayOfTheWeek(day: Int) {
  
  //Write your code inside this function.
  switch aNumber {
    case aNumber == 1 :
     print("Monday")
    case aNumber == 2 :
     print("Tuesday")
    case aNumber == 3 :
     print("Wednesday")
    case aNumber == 4 :
     print("Thursday")
    case aNumber == 5 :
     print("Friday")
    case aNumber == 6 :
     print("Saturday")
    case aNumber == 7 :
     print("Sunday")
    default :
     print("Error")
  }
  
  
}

//Don't change this
dayOfTheWeek(day: aNumber)

Your code did not work because you made it "==" instead of just "=". You also do not need to state "aNumber" after every case. So if you just made the switch statement to be
switch aNumber {

case = 1 :
 print("Monday")
case = 2 :
 print("Tuesday")

and so forth then it would have worked.

@qshepard
Copy link

This was my code and it was wrong

`////Don't change this
var aNumber = Int(readLine()!)!

func dayOfTheWeek(day: Int) {

switch aNumber {

case 1:
print("Monday")
case 2:
print ("Tuesday")
case 3:
print ("Wednesday")
case 4:
print ("Thursday")
case 5:
print ("Friday")
case 6:
print ("Saturday")
case 7:
print ("Sunday")
default: 
print ("Error")

}

}

//Don't change this
dayOfTheWeek(day: aNumber)`

I had to come here to figure out what was wrong and she didnt start the cases at "0" which she says the programming world likes to do. And I started the week at Sunday lol.. but at least I got the logic behind it.

@jihredoy
Copy link

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

@n0xer90
Copy link

n0xer90 commented Apr 19, 2020

////Don't change this
var aNumber = Int(readLine()!)!

func dayOfTheWeek(day: Int) {

//Write your code inside this function.
switch day {
case 5:
print("Friday")
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case 4:
print("Thursday")
case 6:
print("Saturday")
case 7:
print("Sunday")
default:
print("error")
}

}

//Don't change this
dayOfTheWeek(day: aNumber)

@gyos23
Copy link

gyos23 commented May 27, 2020

why the below code just couldn't work?

////Don't change this
var aNumber =  Int(readLine()!)! 

func dayOfTheWeek(day: Int) {
  
  //Write your code inside this function.
  switch aNumber {
    case aNumber == 1 :
     print("Monday")
    case aNumber == 2 :
     print("Tuesday")
    case aNumber == 3 :
     print("Wednesday")
    case aNumber == 4 :
     print("Thursday")
    case aNumber == 5 :
     print("Friday")
    case aNumber == 6 :
     print("Saturday")
    case aNumber == 7 :
     print("Sunday")
    default :
     print("Error")
  }
  
  
}

//Don't change this
dayOfTheWeek(day: aNumber)

Your Code does not work because:

1.) You are trying to use an input parameter that is defined in repl.it in the console input. In repl.it the ' Int(readLine) ' keyword, tells you to put in a value for the parameter, in this case it is an Integer. To give the readLine a value, you'll have to click on the symbol at the upper right corner that looks like this:
Bildschirmfoto 2020-03-10 um 12 13 17

2.) You are using for both, the switch and the case the same argument, this will definitely not work because it does not make any logical sense
You will have to use the input parameter of the function that you are calling at the bottom, which is of course "day". I'll explain you why, so that you can fully understand it.

You have your function 'dayOfTheWeek' with an input parameter called 'day' that takes an Integer, right?
So, now we want to say that when the value of 'day' is equal to '1', we want to print "Monday", right?

Now, the moment of truth:

When you write down the keyword 'switch' , it does not literally mean switch this thing.
It means:
for the thing you write after the switch keyword, in that case 'day' , you want do define some different 'cases', here it is the value for the day, and when the value of ' day ' is equal to ' 1 ' then it executes the code after ' case 1: '
so now we want to say: when the value of 'day' is 1 --> then print "Monday"

So that's why the could for that would look like this:

`////Don't change this
var aNumber =  Int(readLine()!)! 

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

//Don't change this
dayOfTheWeek(day: aNumber)`

Hope that i could help you with that one, don't suffer, programming is not easy at all.
Takes time, be patient :)

Great explanation.

@dandoliniandassoc12
Copy link

I am curious, in which statements do you have to use the if statement vs the switch statement?

@meow-sama
Copy link

Here is my approach...-

////Don't change this
var aNumber = Int(readLine()!)!

func dayOfTheWeek(day: Int) {

//Write your code inside this function.

var theNumber = ["error", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

switch day {
case ..<1:
print("Error")
case 8...:
print("Error")
case day:
print(theNumber[day])
default:
print("Error")
}
}

//Don't change this
dayOfTheWeek(day: aNumber)

@rhenz
Copy link

rhenz commented Jul 3, 2020

Here is my approach...-

////Don't change this
var aNumber = Int(readLine()!)!

func dayOfTheWeek(day: Int) {

//Write your code inside this function.

var theNumber = ["error", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

switch day {
case ..<1:
print("Error")
case 8...:
print("Error")
case day:
print(theNumber[day])
default:
print("Error")
}
}

//Don't change this
dayOfTheWeek(day: aNumber)

I would not suggest this solution as it can confused other people who are trying to read this. Make it simple and readable, don't overcomplicate things.

A simple switch statement like this should be enough for this challenge. It's more readable and understandable.

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

@LalBul
Copy link

LalBul commented Jul 16, 2020

func dayOfTheWeek(day: Int) {

switch day {
case 1:
    print("Monday")
case 2:
    print("Thuesday")
case 3:
    print("Wednestday")
case 4:
    print("Thursday")
case 5:
    print("Friday")
case 6:
    print("Sunday")
case 7:
    print("Saturday")
default:
    break
}

}

dayOfTheWeek(day: 5) // Friday

@MRelindes
Copy link

Hy Guys! That's my code, but didn't work, could someone help me?
I also tried switch day
Thanks!

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

@mandalac
Copy link

////Don't change this
var aNumber = Int(readLine()!)!

func dayOfTheWeek(day: Int) {

//Write your code inside this function.
switch aNumber{
case 1..<2:
print("Monday")
case 2..<3:
print("Tuesday")
case 3..<4:
print("Wednesday")
case 4..<5:
print("Thursday")
case 5..<6:
print("Friday")
case 6..<7:
print("Saturday")
case 7..<8:
print("Sunday")
default :
print("Error")
}

}

//Don't change this
dayOfTheWeek(day: aNumber)

@Judievo
Copy link

Judievo commented Oct 25, 2020

Why do we need the statement : let aNumber = Int(readLine()!)! in every code? because whenever I apply it on my playground. It does not work and always show this.

Screen Shot 2020-10-25 at 22 18 40

When I take it out of my code, it works properly (as below). Can anyone please explain that to me? Many thanks!!

Screen Shot 2020-10-25 at 22 20 01

@divijakollu
Copy link

import UIKit

func dayOfTheWeek(day: Int) {

//Write your code inside this function.
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")
}

//Don't change this
dayOfTheWeek(day: 6)

@CaptainAN
Copy link

I had a little fun with this ...

////Don't change this

func dayOfTheWeek(day: Int) {

//Write your code inside this function.

//Just to check what number this works out to
let wd = "Day \(day) of the week is"
let c = "Result of calculation = \(day%7) ="

//Translating 1...7 into a weekday
switch day {
case 1:
    print("\(wd) Monday")
case 2:
    print("\(wd) Tuesday")
case 3:
    print("\(wd) Wednesday")
case 4:
    print("\(wd) Thursday")
case 5:
    print("\(wd) Friday")
case 6:
    print("\(wd) Saturday")
case 7:
    print("\(wd) Sunday")

//Translating any number above 7 into a weekday

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

}

dayOfTheWeek(day: 1)
dayOfTheWeek(day: 123456)

image

@Dimanikin
Copy link

var aNumber = 3

func dayOfTheWeek(day: Int) {

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

}

dayOfTheWeek(day: aNumber)

@NastasiaIOSdev
Copy link

Снимок экрана 2020-12-27 в 23 49 15

@vido94
Copy link

vido94 commented Dec 29, 2020

Why do we need the statement : let aNumber = Int(readLine()!)! in every code? because whenever I apply it on my playground. It does not work and always show this.

Screen Shot 2020-10-25 at 22 18 40

When I take it out of my code, it works properly (as below). Can anyone please explain that to me? Many thanks!!

Screen Shot 2020-10-25 at 22 20 01

I have the same question what does let aNumber = Int(readLine()!)! do? Thanks

@imnotLeonn
Copy link

why the below code just couldn't work?

////Don't change this
var aNumber =  Int(readLine()!)! 

func dayOfTheWeek(day: Int) {
  
  //Write your code inside this function.
  switch aNumber {
    case aNumber == 1 :
     print("Monday")
    case aNumber == 2 :
     print("Tuesday")
    case aNumber == 3 :
     print("Wednesday")
    case aNumber == 4 :
     print("Thursday")
    case aNumber == 5 :
     print("Friday")
    case aNumber == 6 :
     print("Saturday")
    case aNumber == 7 :
     print("Sunday")
    default :
     print("Error")
  }
  
  
}

//Don't change this
dayOfTheWeek(day: aNumber)

Your Code does not work because:

1.) You are trying to use an input parameter that is defined in repl.it in the console input. In repl.it the ' Int(readLine) ' keyword, tells you to put in a value for the parameter, in this case it is an Integer. To give the readLine a value, you'll have to click on the symbol at the upper right corner that looks like this:
Bildschirmfoto 2020-03-10 um 12 13 17

2.) You are using for both, the switch and the case the same argument, this will definitely not work because it does not make any logical sense
You will have to use the input parameter of the function that you are calling at the bottom, which is of course "day". I'll explain you why, so that you can fully understand it.

You have your function 'dayOfTheWeek' with an input parameter called 'day' that takes an Integer, right?
So, now we want to say that when the value of 'day' is equal to '1', we want to print "Monday", right?

Now, the moment of truth:

When you write down the keyword 'switch' , it does not literally mean switch this thing.
It means:
for the thing you write after the switch keyword, in that case 'day' , you want do define some different 'cases', here it is the value for the day, and when the value of ' day ' is equal to ' 1 ' then it executes the code after ' case 1: '
so now we want to say: when the value of 'day' is 1 --> then print "Monday"

So that's why the could for that would look like this:

`////Don't change this
var aNumber =  Int(readLine()!)! 

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

//Don't change this
dayOfTheWeek(day: aNumber)`

Hope that i could help you with that one, don't suffer, programming is not easy at all.
Takes time, be patient :)

Ok so basically you're saying that 1.- not gonna work because it's like the input is not existing before we type in something so the structure of the program can't have a solid foundation to be ready for what we type in right? and then 2.- we can't assign aNumber as the name of the switch because we're mixing things up right?

My question then is in what moment we create a relation between "day" and "aNumber"?? that's what makes no logic for me. I get aNumber is declared at first as data type and used at the bottom to create the moment in which we can type something in but "day"?? how does it get a value?

@reydaniel
Copy link

Why do we need the statement : let aNumber = Int(readLine()!)! in every code? because whenever I apply it on my playground. It does not work and always show this.
Screen Shot 2020-10-25 at 22 18 40
When I take it out of my code, it works properly (as below). Can anyone please explain that to me? Many thanks!!
Screen Shot 2020-10-25 at 22 20 01

I have the same question what does let aNumber = Int(readLine()!)! do? Thanks

Let me try to answer,
The readLine() is used to read the input from the user, which is in this case is from the keyboard. So if you need an input from the keyboard, use this function. If not, just delete it and replace this code :

dayOfTheWeek(day: aNumber) to dayOfTheWeek(day: 5) // or whatever you want

In this case we need an input to match with the cases from switch statement, that's why we need readLine(). Since the readLine() will return an optional, we need to unwrap it.

For example :

let aNumber = readLine() // it will return optional on the terminal. When you type "hello" from the keyboard, it will return "optional(hello)".

To solve it, add "!" after parentheses.

let aNumber = readLine()! // it will unwrap the value, and return it instead of returning "optional(value)".

Because we need a number from the input, the code should like this :

let aNumber = Int(readLine()!)! // add "!" to unwrap the value, so it will not return an "optional(value)".

But why it still throw an error even the code is correct? That's because this readLine() function need a terminal to work, and Playground doesn't support it. Instead to create a playfround file, create a new macOS project and choose command line tool like this :

image

It will not show an error anymore because Command Line Tool support the terminal environment to read user input.
Hope it will help you 👍

@reydaniel
Copy link

Why do we need the statement : let aNumber = Int(readLine()!)! in every code? because whenever I apply it on my playground. It does not work and always show this.

Screen Shot 2020-10-25 at 22 18 40

When I take it out of my code, it works properly (as below). Can anyone please explain that to me? Many thanks!!

Screen Shot 2020-10-25 at 22 20 01

https://gist.github.com/TheMuellenator/ceed75150b5f7127c8bf38b3564e7ca3#gistcomment-3687911

Hope it will help you 👍

@trickyj
Copy link

trickyj commented Jul 29, 2021

So basically I just did a reset on the challenge page and then just tried again.

Only use this.

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

Screenshot 2021-07-29 at 10 59 40 AM

@sdeweber
Copy link

sdeweber commented Aug 9, 2021

oh my....as usual it would give me errors again, while working in my playground....so came here to check...you all have the same, I mean this one is quite easy....in the end I didn't copy and paste the last }...(smacks own head)

@alim-star
Copy link

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

@Magdalenaspace
Copy link

////Don't change this
var aNumber = Int(5)

func dayOfTheWeek(day: Int) {
switch day {
case 1:print("Monday")
case 2:print("Tuesday")
case 3:print("Wednesday")
case 4:print("Thursday")
case 5:print("Friday")
case 6:print("Saturday ")
case 7:print("Sunday ")
default:print("Error")
}
}
//Try out some different numbers here
//Don't copy the line below into Udemy

dayOfTheWeek(day: 5)

@73307
Copy link

73307 commented Jan 21, 2022

////Don't change this

func dayOfTheWeek(day: Int) {
let day = Int.random(in: 1...7)
switch day {
case 1:
print("monday")
case 2:
print("tuseday")
case 3:
print("wansday")
case 4:
print("tuseday")
case 5:
print("friday")
case 6:
print("seterday")
case 7:
print("sunday")
default:
print("error")
}

}
dayOfTheWeek(day: 4)
//Try out some different numbers here
//Don't copy the line below into Udemy

@PassionHuni
Copy link

Screen Shot 2022-10-03 at 12 15 39 PM

I still keep getting error. what's wrong with my code?

@alexnavter
Copy link

alexnavter commented Oct 16, 2023

var aNumber =  Int(readLine()!)! 

// Function to determine the corresponding day of the week. It takes an integer day as a parameter
func dayOfTheWeek(day: Int) {
    
    switch day {
    case 1:
        print("Monday")
    case 2:
        print("Tuesday")
    case 3:
        print("Wednesday")
    case 4:
        print("Thursday")
    case 5:
        print("Friday")
    case 6:
        print("Saturday")
    case 7:
        print("Sunday")
    default:
        print("Error")
    }
}

// We call the function with the user input integer aNumber as the argument and it prints the day of the week to the console
dayOfTheWeek(day: aNumber)

@aperez-deloitte
Copy link

////Don't change this
var aNumber = Int(readLine()!)!

func dayOfTheWeek(day: Int) {

//Write your code inside this function.

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

@aichohan
Copy link

This is how I did it, it was super fun.

var aNumber = 1 // you can insert your own number here

func dayOfTheWeek(day: Int) {
    
    let daysArray = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    
    switch day {
    case 1...daysArray.count:
        print(daysArray[day - 1])
    default:
        print("Error")
    }
    
    
    
}

dayOfTheWeek(day: aNumber)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment