Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Last active March 5, 2024 17:50
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save TheMuellenator/69efa8bb847c58c6bc01a82d3f30fd34 to your computer and use it in GitHub Desktop.
Save TheMuellenator/69efa8bb847c58c6bc01a82d3f30fd34 to your computer and use it in GitHub Desktop.
iOS repl.it - Variables Challenge Solution
var a = 5
var b = 8
var c = a
a = b
b = c
print("a: \(a)")
print("b: \(b)")
@Alex-Petrenko-21
Copy link

Alex-Petrenko-21 commented Jan 27, 2020

var a = 5
var b = 8

print("The value of a is (a)")
print("The value of a is (b)")

I was kind of confused about what to do with the code and how to manage it without adding a third variable. 😟

@Atiya
Copy link

Atiya commented Feb 3, 2020

var a = 5
var b = 8

print("The value of a is (b)")
print("The value of b is (a)")

Alex. id you mean what I put above? That is what first came to mind to solve it but it was not using 3 lines of code. It is was clear that we were suppose to write 3 lines of code but I was not clear why in the original probalem those print statements were there or what to take out and what to leave in.

@Alex-Petrenko-21
Copy link

Alex-Petrenko-21 commented Feb 4, 2020 via email

@Local911
Copy link

var a = 5
var b = 8
var c = 3

print("a: (a+c)")
print("b: (b-c)")

I guess I cheated a lot lol

@hgunes
Copy link

hgunes commented Feb 22, 2020

A bit longer but still :)
var c = b - a
a = b
b -= c

@luthergu
Copy link

func swap(a:inout Int, b:inout Int){
a = a + b
b = a - b
a = a - b
}
var a = 5
var b = 8

swap(&a, &b)

print("a: (a)")
print("b: (b)")

@davfej
Copy link

davfej commented Mar 23, 2020

var a = 5
var b = 8

print("a: (b)")
print("b: (a)")

/*
print("a: (a)")
print("b: (b)")
*/

@jihredoy
Copy link

var c = a
a = b
b = c

print("a: (a)")
print("b: (b)")

@stanmill
Copy link

var a = 5
var b = 8
a = a + b // a=13
b = a - b //b=5
a = a - b //a=8
print("a: (a)")
print("b: (b)")

@rushabh-hub
Copy link

// Checkout my code as below keep it simple

var a = 5
var b = 8

var c = a + b

//Replace this line with your code.
a = c - a
b = c - b
print("a: (a)")
print("b: (b)")

@krrish-cmd
Copy link

please tell i did it. is it correct

by entering this code between variables and print statement

i entered this :
a = b
b = a

and it worked perfectly

@rushabh-hub
Copy link

Can you please share your complete code @krrish-cmd

Use in between print will work but at what point you changed the Variable need to check.

@keshabraj
Copy link

please tell i did it. is it correct

by entering this code between variables and print statement

i entered this :
a = b
b = a

and it worked perfectly
var a = 5
var b = 8

if you assign a = b that will be 8 and then
again the b which is holding new value 8, you assign it to a,
that means both of these links print the same i.e. 8.

@krrish-cmd
Copy link

krrish-cmd commented Jun 6, 2020 via email

@marcusmania
Copy link

var a = 5
var b = 8

(a, b) = (b, a)

var a = 5
var b = 8

(a, b) = (b, a)

print("The value of a is: (a)")
print("the value of b is: (b)")

Output:
The value of a is: 8
the value of b is: 5

@Rachidgd
Copy link

var a = 5
var b = 8

a += 3
b -= 3

@RodriguesUmbelino
Copy link

var a = 5
var b = 8

var c = b
b = a
a = c
//Replace this line with your code.

print("a: (b)")
print("b: (a)")

@rhenz
Copy link

rhenz commented Jul 1, 2020

I suggest you name your variable like this instead of using "c" or other words. It makes your code much more readable

var a = 5
var b = 8

var tempA = a
a = b
b = tempA

print("a: \(a)")
print("b: \(b)")

@samiparken
Copy link

my answer is one line
var c = a; a = b; b = c;

@jkardach
Copy link

var a = 5
var b = 8

var c = a
a = b
b = c

print("a = (a)")
print("b = (b)")

@iosappsdevelopers
Copy link

var a = 5
var b = 8
a = a+b
b = a-b
a = a-b

print("a = (a)")
print("b = (b)")

@VictorToledoCLF
Copy link

I did it like this xD

var a = 5
var b = 8

a=(a+a+a+a+a+a+a+a)/a
b=(b+b+b+b+b)/b

print("the value of a is (a)")
print("the value of b is (b)")

@Akhmadov
Copy link

Akhmadov commented Nov 22, 2020

var a = 5
var b = 8

b = b-a
a = a + b
b = a - b
print(a,b)

@hiheyhana
Copy link

My solution:

`func exercise() {

var a = 5
var b = 8

let c = a
a = b
b = c

print("a: \(a)")
print("b: \(b)")

}`

@NastasiaIOSdev
Copy link

func exercise() {

var a = 5
var b = 8

var c = a
a = b
b = c

print("a: \(a)")
print("b: \(b)")

}

@lordvidex
Copy link

also nice to know how to do it without a 3rd variable:

a=a+b
b=a-b
a=a-b

This would not work for strings now. Would it?

Copy link

ghost commented Aug 9, 2021

var a = 1
var b = 2
var c = a
a = b
b = c
print("a: (a)")
print("b: (b)")

@lamuqeetios
Copy link

var a = 7
var b = 9
var c = a
b = a
a = b
print("a is = (a)")
print ("b is = (b)")

@Magdalenaspace
Copy link

var a = 7
var b = 8
var c = a
a = b
b = c

@fatmayildiza
Copy link

func exercise() {

var a = 5
var b = 8

//Write your code here. 
//Dont change any of the existing code.


print("a: \(b)")
print("b: \(a)")

}

it is working:))

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