Char comparison not working

I am making a program that will need to compare 2 characters. It would take the last character of the first word and check to see if it is the same as the first character in the second word. I have code that seems to be correct so not really sure where to go from here. The function I feel that is in question is wordChained()->Bool.


struct GameView: View {
    @State var score = 0
    @State var health = 3
    @State private var playerWord = ""
    @State private var prevWord = ""
    @State private var usedwords = [String]()
    
    
    var body: some View {
        
        ZStack{
            LinearGradient(gradient: Gradient(colors: [Color.red, Color.orange]), startPoint: .top, endPoint: .bottom)
                        .edgesIgnoringSafeArea(.vertical)
            
            VStack{
                HStack{
                    Image("chain").scaleEffect(1.3)
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                    Image("chain").scaleEffect(1.3)
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                    Image("chain").scaleEffect(1.3)
                }
                
                HStack{
                    HStack{
                        Text("Score:")
                        Text(String(score))
                    }.padding()
                    
                    HStack{
                        Text("Health:")
                        Text(String(health))
                    }
                }
                
                
                Spacer()
                Spacer()
                
                VStack {
                    
                    if health == 0{
                        NavigationLink(destination: MenuView().navigationBarTitle(Text("x")).navigationBarHidden(true)){
                            Text("YOU LOSE | GO TO MENU").padding()
                                .border(Color.black, width: 2)
                                .scaleEffect(0.8)
                                .accentColor(Color.black)
                        }
                        
                        
                    }
                    else{
                        TextField("Enter a Word", text: $playerWord, onCommit: addNewWord) //<- No `()->Void` here
                                                .padding()
                                                .border(Color.black, width: 2)
                                                .scaleEffect(0.8)
                    }
                    
       
                    List(usedwords, id: \.self){
                        Text($0)
                    }
                    
                }
                
                Spacer()
                
                HStack{
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                    Image("chain").scaleEffect(1.3)
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                    Image("chain").scaleEffect(1.3)
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                }
            }
        }
    }
    
    func addNewWord(){
        
        let answer =
            playerWord.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
        
        guard answer.count > 0 else {
            return
        }
        
        if isValid(possibleWord: answer){
            score += 1
            usedwords.insert(answer, at: 0)
            prevWord = answer
            playerWord = ""
        }
        else{
            health -= 1
            playerWord = ""
        }
    }
    
    func getWords() -> [String]?{
        var arrayOfStrings: [String]?

            do {
                // This solution assumes  you've got the file in your bundle
                if let path = Bundle.main.path(forResource: "english3", ofType: "txt"){
                    let data = try String(contentsOfFile:path, encoding: String.Encoding.utf8)
                    arrayOfStrings = data.components(separatedBy: "\n")
                }
            } catch let err as NSError {
                // do something with Error
                print(err)
            }
        return arrayOfStrings
    }
    
    func wordChained() -> Bool{
        let char1 = prevWord[prevWord.index(prevWord.startIndex, offsetBy: prevWord.count - 1)]
        let char2 = playerWord[playerWord.index(playerWord.startIndex, offsetBy: 0)]
        
        if char1 == char2{
            return true
        }
        else{
            return false
        }
    }
    
    func isValid(possibleWord: String) -> Bool{
        let wordList = getWords()
        if ((wordList?.isEmpty) != nil){
            if wordList!.contains(possibleWord){
                return true
            }
            else{
                return false
            }
        }
        else{
            if wordList!.contains(possibleWord) && wordChained(){
                return true
            }
            else{
                return false
            }
        }
    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        GameView()
    }
}

Can you clarify what is the problem of your current code? What is the expected result? And what is the actual result?

It will continuously take inputs and display it in a list if the input is a real word and if the last letter in the previous word is the same as the first letter in the word just inputted. A successful sequence of inputs should be as follows...

Cow Walk Karate Electric candle... and so on

Sorry, but it's far from clarifying what is the problem of your current code. When you will successfully added info about what is the problem, I would write some replies. What is the expected result? And what is the actual result?

try this:

    func wordChained() -> Bool{
        let char1 = prevWord[prevWord.index(prevWord.startIndex, offsetBy: prevWord.count - 1)]
        let char2 = playerWord[playerWord.index(playerWord.startIndex, offsetBy: 0)]

        if char1.lowercased() == char2.lowercased() {   // <--- here
            return true
        }
        else{
            return false
        }
    }

or just:

    func wordChained() -> Bool{
        let char1 = prevWord[prevWord.index(prevWord.startIndex, offsetBy: prevWord.count - 1)]
        let char2 = playerWord[playerWord.index(playerWord.startIndex, offsetBy: 0)]

        return char1.lowercased() == char2.lowercased()  // <--- here
    }

The point being that "a" is not equal to "A"... your test for equality must match the letter case.

For better readability, you could try:

func wordChained() -> Bool{
        if let char1 = prevWord.last?.lowercased(),
           let char2 = playerWord.first?.lowercased(),
           char1 == char2 {
            return true
        }
        return false
    }

Expected: Input cat; score will go up by 1, health will stay the same. Next input is cow; score will stay the same but health will decrease by 1. Actual result: Input cat; score will go up by 1, health will stay the same. Next input is cow; score will go up the same but health will stay the same.

Char comparison not working
 
 
Q