Coder Social home page Coder Social logo

swift-arraychallenge-lab-ios-0916's Introduction

Shopping Time

Dorothy

Toto, I've got a feeling we're not in Kansas anymore. -Dorothy Gale

Overview

In this lab, you'll write a function that takes in two arguments and returns an array.

Instructions

CuteCat

Our adorable cat, Ginger, has been somewhat of a brat lately. We had a nice shopping list all ready to go which included the items and their quantity we needed at the store. But, Ginger got to it. The list has been torn directly in half, with one piece of paper with just numbers on it (representing the quantity of each item needed) and one piece of paper with just the names of the items on it.

It's your job (the programmer) to create a function which will take in two arguments. One of these arguments will be of type [String] which represents the names of the items. The other argument will be of type [Int] which represents the quantity of each item. Considering the list was split in half, they match up evenly. This function should return back an array of type [String] which represents a complete shopping list.

The complete shopping list should look like this:

["1. 6 Bananas", "2. 4 Apples", "3. 12 Eggs", "4. 4 Rolls"]

If you look in the ViewController.swift file, you should find the two arrays that represent the two pieces of paper.

// Quantity of items
var quantityOfItems: [Int] = [6, 4, 12, 4]
    
// Name of items
var nameOfItems: [String] = ["Bananas", "Apples", "Eggs", "Rolls"]

1 - Create a function called makeShoppingListWithItems(_:quantityOfItems:) which will take in two arguments and return back an array of String's that represent the complete shopping list. The first argument of this function will be of type [String] which will represent the names of our items. The second argument will be of type [Int] representing the quantity of these items. It's your job to implement this function. It should utilize both of these arguments and create an array representing the complete shopping list. It should then return that array.

2 - Call on this function in viewDidLoad() and assign the return value of this function to the shoppingList variable which has already been created for you above the viewDidLoad() method. Pass in the nameOfItems and quantityOfItems variables as arguments to this function when calling on it.

Quick Note. How do you read that method signature I included in instruction #1? The name of this function should be makeShoppingListWithItems, that underscore _ means that the first argument to this function will have NO external name when it is called. The second argument to this function will have an external name of quantityOfItems when it is called.

Here's an example.

func printFavoriteNames(names: [String], colors: [String], numbers: [Int]) {
	// printing all my favorite names, colors and numbers
}

We call on this function like so:

let myFavNames = ["Jim", "Bran", "Sansa"]
let myFavColors = ["Red", "Blue", "Green"]
let myFavNumbers = [7, 742, 5]

printFavoriteNames(myFavNames, colors: myFavColors, numbers: myFavNumbers)

Notice how when calling the function, the first argument's external name is not there but colors: and numbers: is. So if we were to type out this method signature to someone, we would do so like this:

printFavoriteNames(_:colors:numbers:)

View this lesson on Learn.co

View Array Challenge Lab on Learn.co and start learning to code for free.

swift-arraychallenge-lab-ios-0916's People

Contributors

jimcampagno avatar annjohn avatar pletcher avatar ipc103 avatar ticiachance avatar

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.