Giter Site home page Giter Site logo

ios-functions-lab-1's Introduction

Functions Lab 1

Fork and clone this repo. On your fork, answer and commit the follow questions. When you are finished, submit the link to your repo on Canvas.

Question 1

Complete the function so that it will print out total cost after tax. Make sure to call the function afterwards.

let itemCost = 45.0
let nyTax = 0.08775

func totalWithTax() {

}

Then, modify the function you implemented to have a return type of Int, and use an external name that looks more readable. Function calls should look something like "total cost of the item after tax"

Question 2

Convert the the following if/else statement below into function with a String return type.

let todaysTemperature = 72

if todaysTemperature <= 40 {
    print("It's cold out.")
} else if todaysTemperature >= 85 {
    print("It's really warm.")
} else {
    print("Weather is moderate.")
}

Question 3

Write a function named min2 that takes two Int values, a and b, and returns the smallest one.

Function Definition func min2(a: Int, b: Int) -> Int

Example: Input: min2(a:1, b:2)

Output: 1

Question 4

Write a function that takes an Int and returns its last digit. Name the function lastDigit. Use _ to ignore the external parameter name.

Function Definition: func lastDigit(_ number: Int) -> Int

Example: Input: lastDigit(12345)

Output: 5

Question 5

Write a function that takes in any two positive integers and return the sum.

Question 6

Write a function takes in any number grade and returns a corresponding letter grade.

Number Grade Equivalent Letter Grade
100 A+
90 - 99 A
80 - 89 B
70 - 79 C
65 - 69 D
Below 65 F

Question 7

Make a calculator function that takes in three parameters (two numbers and one operator) and returns the answer of the operation.

Operator parameter: (+, -, x, /)

Question 8

Write a function so that it will print out total cost after tip.

let mealCost = 45
let tipPercentage = 0.15

//Write your code below

let myFinalCost = totalWithTip() //Fill in the arguments

Write a function that will print out total cost after tip and tax.

let taxPercentage = 0.09

//Write your code below

let myFinalCostWithTipAndTax = totalWithTipAndTax() //Fill in the arguments in function

Question 9

Implement a function named repeatPrint that takes a string message and a integer count as parameters. The function should print message count number of times and then print a newline.

Example: Input: repeatPrint(message: "+", count: 10)

Output: ++++++++++

Question 10

Write a function named first that takes an Int named n and returns an array with the first n numbers starting from 1.

Function Definition func first(_ n: Int) -> [Int]

Example: Input: first(3)

Output: [1, 2, 3]

Question 11

Write a function that prints the numbers from 1 to x, except:

If the number if a multiple of 3, print "Fizz" instead of the number If the number is a multiple of 5, print "Buzz" instead of the number If the number is a multiple of 3 AND 5, print "FizzBuzz" instead of the number Your function should take in one parameter: x (the number to count up to)

Question 12

Write a function named reverse that takes an array of integers named numbers as a parameter. The function should return an array with the numbers from numbers in reverse order.

Example: Input: reverse([1, 2, 3])

Output: [3, 2, 1]

Question 13

Write a function that prints out the most frequently appearing Int in an array of Int.

Question 14

Write a function that sums all the even indices of an array of Ints.

Question 15

Write a function that flips a dictionary. All of the keys are now values and all of the values are now keys.

Example: Input: [1: "hi", 5: "bye:]

Output: ["hi": 1, "bye": 5]

Bonus

Question 1

Write a function that finds the person with the second highest test score in a Dictionary that maps names to scores.

Example: Input: ["Person 1": 83, "Person 2": 74, "Person 3": 82]

Output: "Person 3"

Question 2

Write a function that determines if a value is inside of array.

Question 3

Write a function takes an Int as input, and returns true if it is even, or false if it is odd. Using your new function, write code that prints out whether dieRoll is even or odd:

let dieRoll = Int(arc4random_uniform(6) + 1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.