Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Last active April 11, 2024 02:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save TheMuellenator/83294ef2c2f606f146e51f4c4e547cab to your computer and use it in GitHub Desktop.
Save TheMuellenator/83294ef2c2f606f146e51f4c4e547cab to your computer and use it in GitHub Desktop.
iOS repl.it - Functions 2 Challenge Solution
//Don't change this code:
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input
add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)
}
//Write your code below this line to make the above function calls work.
func add(n1: Int, n2: Int) {
print(n1 + n2)
}
func subtract(n1: Int, n2: Int) {
print(n1 - n2)
}
func multiply(n1: Int, n2: Int) {
print(n1 * n2)
}
func divide(n1: Int, n2: Int) {
let decimalN1 = Double(n1)
let decimalN2 = Double(n2)
print(decimalN1 / decimalN2)
}
//Don't move or change this code:
calculator()
@AdihakinBiin
Copy link

//Don't change this code:
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)

}

//Write your code below this line to make the above function calls work.

func add(n1: Int, n2: Int){
print(n1 + n2)
}
func subtract(n1: Int, n2: Int){
print(n1 - n2)
}
func multiply(n1: Int, n2: Int){
print(n1 * n2)
}
func divide(n1: Int, n2: Int){

let decimal1 = Double(n1)
let decimal2 = Double(n2)
print(decimal1 / decimal2)
}

//Don't move or change this code:
calculator()

@ciphermute
Copy link

ciphermute commented Apr 22, 2020

What happen to the compiler test website things?

add( n1: 2, n2: 13)
subtract(n1: 10, n2: 2)
multiply(n1: 10, n2: 10)
divide (n1: 10, n2: 5)

add(n1: int, n2: int){
c = n1 + n2
print("Answer: (c)")
}

subtract(n1: int, n2: int){
c = n1 - n2
print("Answer: (c)")
}

multiply(n1: int, n2: int){
c = n1 * n2
print("Answer: (c)")
}

divide(n1: int, n2: int)
{
let c = (double) n1 / (double) n2
print("Answer: (c)")
}

@SergeyTLV
Copy link

//Don't change this code:
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)

}

//Write your code below this line to make the above function calls work.

func add (n1: Int, n2: Int){
print (n1+n2)

}

func subtract (n1: Int, n2: Int){
print (n1-n2)
}

func multiply (n1: Int, n2: Int){
print (n1*n2)
}

func divide (n1: Int, n2: Int){
let div:Double = (Double(n1)/Double(n2))
print (div)
}

//Don't move or change this code:
calculator()

@krrish-cmd
Copy link

please muellenator help. i am not getting what are they asking us to do in this(Functions 2) challenge.please help on krrishsareen@gmail.com

@alexDidLoad
Copy link

alexDidLoad commented Jun 22, 2020

I don't understand either. My solution matched the ones above but I received this error:

main.swift:3:7: warning: initialization of immutable value 'a' was never used; consider replacing with assignment to '' or removing it
let a = Int(readLine()!)! //First input

main.swift:4:7: warning: initialization of immutable value 'b' was never used; consider replacing with assignment to '' or removing it
let b = Int(readLine()!)! //Second input

update
I had commented out lines 6-9 according to the instructions:

"Once you've completed your functions with inputs, you can comment out the lines 6-9 and run the code to check."

the code worked after I deleted "//".

@LalBul
Copy link

LalBul commented Jul 15, 2020

func add(a: Int, b: Int) {
let addSym = a + b
print(addSym)
}

func subtract(a: Int, b: Int) {
let subtractSym = a - b
print(subtractSym)
}

func multiply(a: Int, b: Int) {
let multiplySym = a * b
print(multiplySym)
}

func divide(a: Float, b: Float) {
let divideSym = a / b
print(divideSym)
}

@KiEun-Kwon
Copy link

//Don't change this code:
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)

}

func add (n1: Int, n2: Int){
print("add answer : (n1+n2)")
}

func subtract (n1: Int, n2: Int){
print("subtract answer : (n1-n2)")
}

func multiply (n1: Int, n2: Int){
print("multiply answer : (n1*n2)")
}

func divide (n1: Int, n2: Int){
print("divide answer : (Double(n1)/Double(n2))")
}

//Don't move or change this code:
calculator()

@MartyLaba
Copy link

func addNumbers(a: Int, b: Int) -> Int {
    return a + b
}
print(addNumbers(a: 3, b: 4))

func minusNumbers(a: Int, b: Int) -> Int {
    return a - b
}
print(minusNumbers(a: 3, b: 4))

func multiplyNumbers(a: Int, b: Int) -> Int {
    return a * b
}
print(multiplyNumbers(a: 3, b: 4))

func divideNumbers(a: Float, b: Float) -> Float {
    return a / b
}
print(divideNumbers(a: 3, b: 4))

@NDX54
Copy link

NDX54 commented Nov 15, 2020

//Don't change this code:
func calculator() {
  let a = Int(readLine()!)! //First input
  let b = Int(readLine()!)! //Second input

  add(n1: a, n2: b)
  subtract(n1: a, n2: b)
  multiply(n1: a, n2: b)
  divide(n1: a, n2: b)

}

//Write your code below this line to make the above function calls work.


func add(n1: Int, n2: Int) {
    let n3 = n1 + n2

    print(n3)
}

func subtract(n1: Int, n2: Int) {
    let n3 = n1 - n2
    
    print(n3)
}

func multiply(n1: Int, n2: Int) {
    let n3 = n1 * n2
    
    print(n3)
}

func divide(n1: Int, n2: Int) {
    let n3 = (Double(n1)) / (Double(n2))
    
    print(n3)
}

calculator()

@Dimanikin
Copy link

//Don't change this code:
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)

}

//Write your code below this line to make the above function calls work.

func add(n1:Int, n2:Int) {
print(n1 + n2)
}

func subtract(n1:Int, n2:Int) {
print(n1 - n2)
}

func multiply(n1:Int, n2:Int) {
print(n1 * n2)
}

func divide(n1:Int, n2:Int) {
print(Double(n1) / Double(n2))
}

@NastasiaIOSdev
Copy link

//Don't change this code:
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)

}

//Write your code below this line to make the above function calls work.
func add(n1: Int, n2: Int) {
print(n1 + n2)
}

func subtract(n1: Int, n2: Int) {
print(n1 - n2)
}

func multiply(n1: Int, n2: Int) {
print(n1*n2)
}

func divide(n1: Int, n2: Int) {
if n2 != 0 {
print( Double(n1) / Double(n2))
}
}

@divyarana
Copy link

Why have you converted into Double for division. Cann't that be converted to Float?

@IslombekAzamjonov
Copy link

//Don't change this code:
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)

}

//Write your code below this line to make the above function calls work.
func add(n1: Int, n2: Int) {
print(n1 + n2)
}

func subtract(n1: Int, n2: Int) {
print(n1 - n2)
}

func multiply(n1: Int, n2: Int) {
print(n1 * n2)
}

func divide(n1: Int, n2: Int) {

print(decimalN1 / decimalN2)
}

@JCammon1
Copy link

JCammon1 commented Jun 6, 2021

//Don't change this code:
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)

}

//Write your code below this line to make the above function calls work.
func add(n1: Int, n2: Int) {
print(n1 + n2)
}

func subtract(n1: Int, n2: Int) {
print(n1 - n2)
}

func multiply(n1: Int, n2: Int) {
print(n1*n2)
}

func divide(n1: Int, n2: Int) {
if n2 != 0 {
print( Double(n1) / Double(n2))
}
}

I'm not understanding how this works. There aren't even any numbers in here. I feel like we are being asked to do things on these challenges that we are not taught. Is there a video of someone explaining this?

@JCammon1
Copy link

JCammon1 commented Jun 6, 2021

//Don't change this code:
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)

}

func add(n1: Int, n2: Int)
{
let c = n1 + n2 ;
print(c);
}
func subtract(n1: Int, n2: Int)
{
let c = n1 - n2 ;
print(c);
}
func multiply(n1: Int, n2: Int)
{
let c = n1 * n2 ;
print(c);
}
func divide(n1: Int, n2: Int)
{
let c = Double(n1) / Double(n2) ;
print(c);
}

//Don't move or change this code:
calculator()

Were we taught that add and subtract were functions already? What about the "Double?" What is that and why is it needed? I also don't see any integers in any of this code, so how does it come out with numbers? What is "Int(readLine()!)!"? and what does it mean? I don't see how we are supposed to come up with this code on our own with so many new things in it. And I'm still not clear about it after seeing it. I don't understand how you get the results. Can you show the results of this code that you have put up?

@JCammon1
Copy link

JCammon1 commented Jun 6, 2021

//Don't change this code:
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)

}

//Write your code below this line to make the above function calls work.
func add(n1: Int, n2: Int) {
print(n1 + n2)
}

func subtract(n1: Int, n2: Int) {
print(n1 - n2)
}

func multiply(n1: Int, n2: Int) {
print(n1 * n2)
}

func divide(n1: Int, n2: Int) {

print(decimalN1 / decimalN2)
}

Please help me understand how the code you put up produces numbers when there are no numbers in the code. Also please explain the "Double". also, did we even learn about decimals? How is N2 an Int, but comes out as a decimal?

@mtsaccorsi
Copy link

//Don't change this code:
func calculator() {
  let a = 3 //example first input
  let b = 4 //example second input
  
  add(n1: a, n2: b)
  subtract(n1: a, n2: b)
  multiply(n1: a, n2: b)
  divide(n1: a, n2: b)
  
}

//Write your code below this line to make the above function calls work.

func add(n1: Int, n2: Int) {
    print(n1 + n2)
}

func subtract(n1: Int, n2: Int) {
    print(n1 - n2)
}

func multiply(n1: Int, n2: Int) {
    print(n1 * n2)
}

func divide(n1: Int, n2: Int) {
    print(Double(n1) / Double(n2))
}

//Don't move or change this code:
calculator()

@sdeweber
Copy link

sdeweber commented Jul 20, 2021

EDIT: I restarted Xcode today and it ran just fine now with NO changes!
Also, for the code to work on udemy, it accepted the solution when I deleted the calculator() call.


I also don't understand what's wrong... I have the same code for the 4 functions (well, not the double, but that's not the issue here), but I have the same warning message as above about the 'a' and 'b' not used before and no results....

//Don't change this code:
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)

}

//Write your code below this line to make the above function calls work.

func add(n1: Int, n2: Int) {
print(n1 + n2)
}

func subtract(n1: Int, n2: Int) {
print(n1 - n2)
}

func multiply(n1: Int, n2: Int) {
print(n1 * n2)
}

func divide(n1: Int, n2: Int) {
let decimalN1 = Double(n1)
let decimalN2 = Double(n2)
print(decimalN1 / decimalN2)
}

calculator()

@nghejunior
Copy link

func add(n1: Int, n2: Int) {
print(n1 + n2)
}

func subtract(n1: Int, n2:Int) {
print(n1 - n2)
}

func multiply(n1: Int, n2: Int) {
print(n1 * n2)
}

func divide(n1: Int, n2: Int) {

print("\(Double(n1)/Double(n2))")

}

Copy link

ghost commented Aug 16, 2021

//Don't change this code:
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)
}
//Write your code below this line to make the above function calls work.
func add(n1:Int, n2:Int){
print(n1 + n2)
}
func subtract(n1:Int, n2:Int){
print(n1 - n2)
}
func multiply(n1:Int, n2:Int){
print(n1 * n2)
}
func divide(n1:Int, n2:Int){
print(Float(n1) / Float(n2))
}
//Don't move or change this code:
calculator()

Copy link

ghost commented Aug 16, 2021

func add(n1: Int, n2: Int) {
print(n1 + n2)
}

func subtract(n1: Int, n2: Int) {
print(n1 - n2)

func multiply(n1: Int, n2: Int) {
print(n1 * n2)
}

func divide(n1: Int, n2: Int) {
let decimalN1 = Double(n1)
let decimalN2 = Double(n2)
print(decimalN1 / decimalN2)
}
//Don't move or change this code:
calculator()
Screenshot 2021-08-16 at 3 52 29 PM

@AleksandraDemidenko
Copy link

//Don't change this code:
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input
add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)
}
//Write your code below this line to make the above function calls work.
func add(n1: Int, n2: Int) {
print(n1 + n2)
}
func subtract(n1: Int, n2: Int) {
print(n1 - n2)
}
func multiply(n1: Int, n2: Int) {
print(n1*n2)
}
func divide(n1: Int, n2: Int) {
if n2 != 0 {
print( Double(n1) / Double(n2))
}
}

I'm not understanding how this works. There aren't even any numbers in here. I feel like we are being asked to do things on these challenges that we are not taught. Is there a video of someone explaining this?

Hi! I also don't understand if you found a video that explains this, could you please share? Thank you in advance!

@Mehdi-Tazi
Copy link

hey guys I've been running this on a playground but how the hello do you run the function ????

I've tried this :

calculator(n1: 2, n2: 3)

but I DONT GET ANYTHING

@73307
Copy link

73307 commented Jan 22, 2022

//Don't change this code:

func calculator (){
let n1 = 3
let n2 = 4

print(n1+n2)
print(n1-n2)
print(n1*n2)
let decimalN1 = Double(n1)
let decimalN2 = Double(n2)
print(decimalN1/decimalN2)

}

//Don't move or change this code:
calculator()

@R-Ivan-V
Copy link

R-Ivan-V commented Feb 7, 2022

`let a = 3 //First input
let b = 4 //Second input

func add(n1: Int, n2: Int) {
print(n1 + n2)
}

func subtract(n1: Int, n2: Int) {
print(n1 - n2)
}

func multiply(n1: Int, n2: Int) {
print(n1 * n2)
}

func divide(n1: Int, n2: Int) {
print(Double(n1) / Double(n2))
}
`

@pa1ar
Copy link

pa1ar commented Apr 23, 2022

ok this is a rather confusing challenge, not because it's hard, but because it kinda does not fit to the place where you are in the course.

first off, the solution DOES NOT work in the playground (i don't know why). so you kinda stuck with repl.it.

then, this challenge uses USER INPUT, which wasn't explained in the course before. that is why many people are confused with the absence of any numbers inside of the code.

so here:

let a = Int(readLine()!)! //First input
  let b = Int(readLine()!)! //Second input

the function reads the input, which you suppose to provide in console after running the function WITHOUT any arguments, as there are no parameters required.

this part

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)

calls function, which you declare AFTER the function. that does not matter, as you call the function also AFTER you add missing functions.

the functions, which you need to write are as follows:

func add(n1: Int, n2: Int) {
 print(n1 + n2)
}

func subtract(n1: Int, n2: Int) {
 print(n1 - n2)
}

func multiply(n1: Int, n2: Int) {
 print(n1 * n2)
}

func divide(n1: Int, n2: Int) {
 let decimalN1 = Double(n1)
 let decimalN2 = Double(n2)
 print(decimalN1 / decimalN2)
}

you need to convert to double, because otherwise only the first digit will be counted as answer (0.75 -> 0). it is not rounding up, that is why you don't see 1 as you might expect.

and to launch this all you need to add

calculator()

in the end.

@frioanbia
Copy link

//Don't change this code:
func calculator() {
let a = 3 //First input
let b = 4 //Second input

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)

}

//Write your code below this line to make the above function calls work.
func add(n1: Int, n2: Int) {
print(n1 + n2)
}

func subtract(n1: Int, n2: Int) {
print(n1 - n2)
}

func multiply(n1: Int, n2: Int) {
print(n1 * n2)
}

func divide(n1: Int, n2: Int) {
print(Double(n1) / Double(n2))
}

calculator()

@aperez-deloitte
Copy link

//Don't change this code:
func calculator() {
let a = Int(readLine()!)! //First input
let b = Int(readLine()!)! //Second input

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)

}

//Write your code below this line to make the above function calls work.

func sum(n1: Int, n2: Int) {
print(n1 + n2)
}

func subtract(n1: Int, n2: Int) {
print(n1 - n2)
}

func multiply(n1: Int, n2: Int) {
print(n1 * n2)
}

func divide(n1: Int, n2: Int) {
let decN1 = Double(n1)
let decN2 = Double(n2)
print(decN1 / decN2)
}

@HyperKoder
Copy link

ok this is a rather confusing challenge, not because it's hard, but because it kinda does not fit to the place where you are in the course.

first off, the solution DOES NOT work in the playground (i don't know why). so you kinda stuck with repl.it.

then, this challenge uses USER INPUT, which wasn't explained in the course before. that is why many people are confused with the absence of any numbers inside of the code.

so here:

let a = Int(readLine()!)! //First input
  let b = Int(readLine()!)! //Second input

the function reads the input, which you suppose to provide in console after running the function WITHOUT any arguments, as there are no parameters required.

this part

add(n1: a, n2: b)
subtract(n1: a, n2: b)
multiply(n1: a, n2: b)
divide(n1: a, n2: b)

calls function, which you declare AFTER the function. that does not matter, as you call the function also AFTER you add missing functions.

the functions, which you need to write are as follows:

func add(n1: Int, n2: Int) {
 print(n1 + n2)
}

func subtract(n1: Int, n2: Int) {
 print(n1 - n2)
}

func multiply(n1: Int, n2: Int) {
 print(n1 * n2)
}

func divide(n1: Int, n2: Int) {
 let decimalN1 = Double(n1)
 let decimalN2 = Double(n2)
 print(decimalN1 / decimalN2)
}

you need to convert to double, because otherwise only the first digit will be counted as answer (0.75 -> 0). it is not rounding up, that is why you don't see 1 as you might expect.

and to launch this all you need to add

calculator()

in the end.

still don't get it. Could you explain in more detail? Appreciated!

@Devavrat01
Copy link

func addation() {
print("(a + b)")
}
var a = 3
var b = 4
addation()

func subtraction() {
print("(a - b)")
}
var a = 3
var b = 4
subtraction()
func multiply()
{
var a = 3
var b = 4
print("(a * b)")
}
multiply()

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