Arrays and Loops

// This is how an array is defined
// use let for a inmutable array or var if you plan to change the information

let highScores = [100, 123, 118, 288, 187, 817, 30, 50, 100] 
var studentNamesInClass = ["Student 1", "Student 2", "Student 3", "Student 4", "Student 5"]

type(of: highScores) // this will let you know what type of array is highScores
var numberOfItems = highScores.count // this will return an Int telling you how many items are in the array and assign the number to a var called numberOfItems

var name = "Julio"
var lettersCount = name.count // a string variable can also be considered an array 


var weekdays = ["monday", "tuesday", "wednesday", "thursday", "friday"]
weekdays[0] //this will return monday, arrays start at 0
weekdays.append("Saturday") //this will add Saturday to the array at index 5
weekdays.contains("monday") //this will return a bool in this case true

for loop

let friends = ["name1", "Name2", "Name3", "Name4", "Name5"]

func invite(friend: String) {
    print("Hey, \(friend), please come to my party on Friday!")
invite(friend: friends[2]) //This will call the function invite and print the name at index 2

func invite2(friends: [String]) { 
    for friend in friends {
        print("Hello  " + friend)
invite2(friends: friends) //This function is better it loops through the list and prints every name in the array
//This code will search an array and print the location of the item in the array
var flavors = ["Chocolate", "Vanilla", "Strawberry", "Pistachio", "Rocky Road"]
let userInput = "Rocky Road"
var count = Int()
var userInputLocation = Int()
for flavor in flavors {

    if flavor != userInput {
        count += 1
    } else if flavor == userInput {
        print("found \(userInput) at array location \(count)")
        userInputLocation = count

flavors[userInputLocation] = "Mazapan" //this will replace the item searched with the new string
//this code will make sure program do not go out of bounds
let devices = ["iPhone", "iPad", "iPod", "iMac"]

let index = 0
if index >= devices.count {
    print("you are out of bound")
} else {
var karaokeSongs = [String]()

karaokeSongs.append("Song Number 1")
karaokeSongs.append("Song Number 2")
karaokeSongs.append("Song Number 3")

 - callout(Exercise):
 One enthusiastic singer wants to add three songs at once. Create an array holding this one singer's song list and use the `+=` operator to append their whole list to the end of the group's song list.
var visitor1 = ["Visitor Song 1", "Visitor Song 2", "Visitor Song 3"]

karaokeSongs += visitor1

 - callout(Exercise):
 Write a `for…in` loop and, for every song title in the array, print an encouraging announcement to let the next singer know that it's their turn.

for song in karaokeSongs {
    print("We are now playing \(song)")

 After the loop has called everyone up to sing, use the `removeAll` method on the song list to clear out all the past songs.
//vote count function
func printResults(forIssue messure : String, withVotes votes : [Bool]) -> String {
    var yesVotes = Int()
    var noVotes = Int()
    var totalVotes = Int()
    for vote in votes {
        if vote == true {
            yesVotes += 1
            totalVotes += 1
        } else {
            noVotes += 1
            totalVotes += 1
    print("\(messure) \(yesVotes) yes, \(noVotes) no. \nTotal Votes \(totalVotes)")
    if yesVotes > noVotes {
        return "\(messure) PASSED "
    } else if yesVotes < noVotes {
        return "\(messure) DID NOT PASSED "
    } else {
        return "\(messure) TIE NEW VOTE NECESARY "
import Foundation

var oneMessage = aliceMessages[122]

//find a character in a story
func lookForCharacter (name: String, story: [String]) {
    var index = Int()
    var linesWhereCharacterApears = [Int]()
    for line in story {
        if line.contains(name) {
            index += 1
        } else {
            index += 1

lookForCharacter(name: "Caterpillar", story: aliceMessages)

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *