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 

Indices

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
    }

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

let index = 0
if index >= devices.count {
    print("you are out of bound")
} else {
    print(devices[index])
}
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.
 */
karaokeSongs.removeAll()
//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

aliceMessages
var oneMessage = aliceMessages[122]
print(oneMessage)
oneMessage.contains("Alice")


//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) {
            print("Yes")
            linesWhereCharacterApears.append(index)
            index += 1
        } else {
            print("no")
            index += 1
        }
    }
    print(linesWhereCharacterApears)
}

lookForCharacter(name: "Caterpillar", story: aliceMessages)
aliceMessages[152]

Similar Posts

Leave a Reply

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