Need help for lesson 14

hi guys

I really thought about This question, but I couldn't answer that

Could you please tell me what is the solution or how i can solve it?


in Intro to App Development Curriculum lesson 14


Exercise: Counting Votes

You're implementing a poll app for your class. You start with a basic yes-no question counter and now you have your first batch of answers back, parsed into arrays below.

This is a lot of data! But don't worry too much about the long arrays. Whether you're planning to loop over two items or two thousand, you’ll write the loop in exactly the same way.

let shouldMascotChangeVotes: [Bool] = [false, false, false, true, false, true, true]
let shouldInstallCoffeeVendingMachineVotes: [Bool] = [true, true, false, false]
let shouldHaveMorePollOptionsVotes: [Bool] = [false, false, true, true, false, true, false, false, false, false]

This is too many votes to tally quickly by hand, so you’ll write some code to tally it for you.

NOTE

This is also a lot of votes for Swift to use type inference to determine what kind of array it has. The type annotation is written in the array literals above to tell Swift the type of array. This prevents the playground from running slowly.

Accepted Reply

No, your function must be generic, that mean you must be able to use it for different types of questions.

In addition, the parameters must be defined by their type, not with a value (except, you will see later, to define default values, but that's another point)

And now, you should count the votes in the function.


So, something like :


func printResults(forIssue: String, withVotes: [Bool] ) {
     var yesVotes = 0
     var noVotes = 0
    for voteOne in withVotes {
        if voteOne {
            yesVotes = yesVotes + 1
        } else {
            noVotes = noVotes + 1
        }
    }
     print("Result for issue : ", forIssue, "yes: ", yesVotes, "no: ", noVotes)
}

And you call it :

printResults(forIssue: "Should we change the mascot?", withVotes: shouldMascotChangeVotes)



Note that you could also ask the function to return the count and use theis result for printing, outside the function ; then function must return the 2 values for yes and no count


func countResults(forIssue: String, withVotes: [Bool]) -> (yesCount: Int, noCount: Int){
     var yesVotes = 0
     var noVotes = 0
    for voteOne in withVotes {
        if voteOne {
            yesVotes = yesVotes + 1
        } else {
            noVotes = noVotes + 1
        }
    }
    return (yesVotes, noVotes)
}



then you use it that way :

let (yesVotesCount, noVotesCount) = countResults(forIssue: "Should we change the mascot?", withVotes: shouldMascotChangeVotes) // Note how to define the tuple  (yesVotesCount, noVotesCount) to store the result returned by the func.
print("Result for issue : ", forIssue, "yes: ", yesVotesCount, "no: ", noVotesCount)

Replies

Exercise

Create two variables, one to count yes votes and one to count no votes. Each should start off with a value of zero.

________ExerciseCreate a for…in loop that loops over one of the vote collections and checks the value of each vote. If the vote is true, the loop should add one vote to the yes variable. If it's false, it should add one vote to the no variable.

________ExerciseAfter the loop has finished, write an if statement that compares the two values and prints a different message based on whether the vote passed or failed.

_________ExerciseTest your code by calling the for…in loop on each of the vote collections.Which measures won by popular vote?

Extension:

Your for…in loop would be even more powerful if you could easily reuse it. The easiest way to reuse code is to put it in a function.

That's an exercise, it is important that you practise yourself and not just get the answer from someone.


What have you written so far for each question ?


To give a little start help, to declare a var with an initial value of 0, you write


var yesCount : Int = 0

Do the same for no.


And continue your exercise.


Note : this is the wrong foruim, it is less a question about Swift than about getting stated.

I road this code for first part:


var yes = 0
var no = 0


for i in shouldMascotChangeVotes {
    if shouldMascotChangeVotes.contains(true)  {
        yes = yes + 1
    } else {
        no = no + 1
    }
}
for i in shouldInstallCoffeeVendingMachineVotes {
    if shouldInstallCoffeeVendingMachineVotes.contains(true)  {
        yes = yes + 1
    } else {
        no = no + 1
    }
}
for i in shouldHaveMorePollOptionsVotes {
    if shouldHaveMorePollOptionsVotes.contains(true)  {
        yes = yes + 1
    } else {
        no = no + 1
    }
}



it doesnt work and i dont understand it totally

When you say "doesn't work", what do you mean ? What do you get ?


But there are errors in your code:


for i in shouldMascotChangeVotes {
    if shouldMascotChangeVotes.contains(true)  {
        yes = yes + 1
    } else {
        no = no + 1
    }
}

shouldMascotChangeVotes is an array that contains some true : so the test shouldMascotChangeVotes.contains(true) is always true, and you add to yes each time.

In addition, you do not use i.


So, you should write :


or vote in shouldMascotChangeVotes {     // explore each item in the array
    if vote  {
        yes = yes + 1
    } else {
        no = no + 1
    }
}
print("yes ", yes, "no ", no)     // To print the result in the console


Correct the others and see if it works.


Don't forget to reset yes and no to 0 !

Well DONE

Excellent


I solved two next part and they worked great

but here is my misunderstanding again

the last part:

Write a function that takes two arguments: a string describing the issue being voted on and an array with the issue's Bool votes. Move the for…in loop and the if statement inside the function, so it prints the results when you call the function with a particular issue's arguments. You should be able to call the function like this:

printResults(forIssue: "Should we change the mascot?", withVotes:shouldMascotChangeVotes)

A message like this should be printed to the console:

Should we change the mascot? 54 yes, 23 no

func shouldMascotChange(forIssue: "Should we change the mascot?", withVotes: shouldMascotChangeVotes ) { //shall I write the argument like that?
    for voteOne in shouldMascotChangeVotes {
        if voteOne {
            yesOne = yesOne + 1
        } else {
            noOne = noOne + 1
        }
    }
}

No, your function must be generic, that mean you must be able to use it for different types of questions.

In addition, the parameters must be defined by their type, not with a value (except, you will see later, to define default values, but that's another point)

And now, you should count the votes in the function.


So, something like :


func printResults(forIssue: String, withVotes: [Bool] ) {
     var yesVotes = 0
     var noVotes = 0
    for voteOne in withVotes {
        if voteOne {
            yesVotes = yesVotes + 1
        } else {
            noVotes = noVotes + 1
        }
    }
     print("Result for issue : ", forIssue, "yes: ", yesVotes, "no: ", noVotes)
}

And you call it :

printResults(forIssue: "Should we change the mascot?", withVotes: shouldMascotChangeVotes)



Note that you could also ask the function to return the count and use theis result for printing, outside the function ; then function must return the 2 values for yes and no count


func countResults(forIssue: String, withVotes: [Bool]) -> (yesCount: Int, noCount: Int){
     var yesVotes = 0
     var noVotes = 0
    for voteOne in withVotes {
        if voteOne {
            yesVotes = yesVotes + 1
        } else {
            noVotes = noVotes + 1
        }
    }
    return (yesVotes, noVotes)
}



then you use it that way :

let (yesVotesCount, noVotesCount) = countResults(forIssue: "Should we change the mascot?", withVotes: shouldMascotChangeVotes) // Note how to define the tuple  (yesVotesCount, noVotesCount) to store the result returned by the func.
print("Result for issue : ", forIssue, "yes: ", yesVotesCount, "no: ", noVotesCount)

THANK YOU VERY MUCH

You welcome. And good continuation.

I don´t really understans what is voteOne.

could you help me

thanks

Hy there,

What do you think about this solution:

var countYes = 0

var countNo = 0

for value in shouldInstallCoffeeVendingMachineVotes {

    let countVotes = "\(value)"

    if countVotes.hasPrefix("true") {

        countYes += 1

    } else { countNo += 1

    }

}

if countYes > countNo {

    print("We have a winner.\nWe have count \(countYes) true votes and \(countNo) false votes.")

} else if countYes < countNo {

    print("Sorry, but the vote didn't passed.\nWe have count \(countYes) true votes and \(countNo) false votes.")

} else {

    print("We have an equity, so we have to vote again.\nWe have count \(countYes) true votes and \(countNo) false votes.")

}
IMHO, this is not a very good code.

What is the interest to transform a Bool into String to later look for "true" or "false", instead of testing the Bool directly ?

Note: when you post code, use the code formatter tool (<>) to present it properly.