Could you please help me with this exercise in Swift Playground?

This is an exercise:

  1. Create an array literal with 20 to 25 items of sample data for your daily activity. It may be something like let milesBiked = [3, 7.5, 0, 0, 17 ... ] Feel free to make up or embellish the numbers, but make sure you have entries that are above, below and exactly at the goal you've thought of. Hint: Make sure to choose the right kind of array for your data, whether [Double] or [Int].

  2. Write a function that takes the daily number as an argument and returns a message as a string. It should return a different message based on how close the number comes to your goal. You can be as ambitious and creative as you'd like with your responses, but make sure to return at least two different messages depending on your daily progress!

I've created an array:

let dailySteps = [10000, 332, 9485, 212, 5765, 3423, 9535, 5466, 2133, 9999, 13134, 7674, 9128, 12122, 12121, 100, 27183, 10004, 6521, 7318]

And I've created this function:

func myProgress(number: Int) {

    for distance in number {

            if distance > 10000 {

                print("Yor are better then I thought!")

            } else if distance ==  10000 {

                print("You have reached your goal!")

            } else {

            print("You need to walk more")

        }

    }

}

myProgress(number: dailySteps)

But I get errors like:

  1. <For-in loop requires 'Int' to conform to 'Sequence'> in the second line of my function
  2. <Cannot convert value of type '[Int]' to expected argument type 'Int'> when I try to call my function in the last line

I don't understand what mistakes I've just done

Could someone please explain me my mistake and explain how to make my code to work correctly? :)

Answered by Claude31 in 708858022

The error:

func myProgress(number: Int) {

    for distance in number {

That uses a single number, hence for does not work.

You need to pass an array:

func myProgress(number: [Int]) {

    for distance in number {

In addition, that is what you pass in the call : dailySteps is an [Int]

myProgress(number: dailySteps)

Note: you'd better name parameter as numbers instead of number, for clarity.

Or, to fulfill the goal of exercice, you could write :

func myProgress(number: Int) {

            if number > 10000 {
                print("Yor are better then I thought!")
            } else if number ==  10000 {
                print("You have reached your goal!")
            } else {
            print("You need to walk more")
        }
    }

}

But then call a number from dailySteps

for distance in dailySteps {
  myProgress(number: distance)
}
Accepted Answer

The error:

func myProgress(number: Int) {

    for distance in number {

That uses a single number, hence for does not work.

You need to pass an array:

func myProgress(number: [Int]) {

    for distance in number {

In addition, that is what you pass in the call : dailySteps is an [Int]

myProgress(number: dailySteps)

Note: you'd better name parameter as numbers instead of number, for clarity.

Or, to fulfill the goal of exercice, you could write :

func myProgress(number: Int) {

            if number > 10000 {
                print("Yor are better then I thought!")
            } else if number ==  10000 {
                print("You have reached your goal!")
            } else {
            print("You need to walk more")
        }
    }

}

But then call a number from dailySteps

for distance in dailySteps {
  myProgress(number: distance)
}
Could you please help me with this exercise in Swift Playground?
 
 
Q