Skip to content

Coding with Swift

Lesson 1

Challenge 1

Copy the code and paste to your Playground.

import UIKit

var number = 0

func addOne() {
    number = number + 1
}

func timesTwo() {
    number = number * 2
}

func printX() {
    print(number)
}

// Do not edit the code above this line
// There are three functions in this code.
// addOne() adds 1 to the current number.
// timesTwo() multiply 2 to the current number
// printX() display the number on the console.

Write codes to print the follow numbers:

  • 2
  • 5
  • 27
  • 100

Challenge 2

Copy the code and paste to your Playground.

import UIKit

var number = 0

func addOne() {
    number = number + 1
}

func timesTwo() {
    number = number * 2
}

func printX() {
    print(number)
}

// Do not edit the code above this line
// There are three functions in this code.
// addOne() adds 1 to the current number.
// timesTwo() multiply 2 to the current number
// printX() display the number on the console.

addOne()
addOne()
addOne()
timesTow()
timesTwo()
timesTwo()
print()

Can you edit the code to make it print 24 successfully to the console?

Challenge 3

Create a function that multiply the current number by 3.