BringTheGnar
BringTheGnar

Reputation: 31

Binary operator '==' cannot be applied to operands of type '[String]' and 'String

I am new to coding and I needed some help with answering this challenge.

Instructions: 
// Given the two arrays below, write a function that takes a String as an input parameter and returns a Boolean value. The function should return true if the String input is in either array and it should return false if the String input is in neither array.
//
// Examples:
// Call your function and pass in the String "cat" as the input. Your function should return true
// Call your function and pass in the String "cow" as the input. Your function should return false

let array1 = ["dog", "cat", "bird", "pig"]
let array2 = ["turtle", "snake", "lizard", "shark"]

// Write your function below:

Here is where I have written my function:

func test(Animal:String){
    let list = array1 + array2
    for animal in list {
        if list == animal {
            print("true")
        } else {
            print("false")
        }
    }
}
test(Animal: "dog")

The error I am getting is:

Binary operator '==' cannot be applied to operands of type '[String]' and 'String under the if statement.

Help if you can and I apologize in advance if this is not formatted correctly.

Thanks!

Upvotes: 1

Views: 11236

Answers (1)

gadu
gadu

Reputation: 1826

Just a tip, in the future you should try adding a more descriptive question title, that is super vague and could mean anything.

Anyways your issue is with the line:

if list == animal {

The error you got is quite specific and tells you exactly what's going wrong. [String] is an array of strings while String is just a single item. So when you write for animal in list your taking one animal in that list at a time.

Comparing an animal to a list is not allowed. They are not the same type ([String] and String are different types, as the compiler error told you). You can only compare variables of the same type. So in your case a list (String array aka [String]) to a another list, or an animal (String) to an animal.

Now what you want to do is see if a string you got is in either array. There's actually a built in method in arrays that lets you do exactly this: .contains

func test(animal:String){
    let list = array1 + array2
    if list.contains(animal) {
        print("true")
    } else {
        print("false")
    }
}

or if you want to be extra concise with some good code you can try

func test(animal:String){
    let list = array1 + array2
    print(list.contains(animal)) // will be false if it doesn't so prints false, true if it does so prints true.
}

And another side note.. You want to be very careful doing an if else inside of a for loop where both the if and the else return. You shouldn't be making decisions after looking at only ONE element.

Because typically you want to check EVERY value before printing false, but when you have an if else, you'll return/print after checking ONLY the first value.

if you wanted to do it your way (no built in methods, iterating through the array) you would want to do something like this:

func test(animal:String){
    let list = array1 + array2
    for candidate in list {
        if animal == candidate { // there's a match! we can return true
            print("true ")
            return // return says to exit the function since we no longer need to keep going this is appropriate
        }
    } // for loop ends

    // we only return false once the ENTIRE for loop has run, because we know if it did exist, we would never get to this point in the code
    // this part only runs once EVERY item has been checked and none were a match
    print("false")
}

And finally as bilal says, never start variable names with capital letters. Typically capital letters mean its a type (like String). You'll notice I renamed Animal -> animal for you in my examples

Upvotes: 4

Related Questions