Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Last active March 26, 2024 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save TheMuellenator/ceed75150b5f7127c8bf38b3564e7ca3 to your computer and use it in GitHub Desktop.
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)
@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