Why code doesn't print the message once?

My code is printing the message multiple times rather than just once when looping through a JavaScript array, how to fix this? Thanks
CodePen

var cars = ["fiat", "ford", "bmw", "audi"]
for(i=0; i<cars.length; i++) {
    if (cars[i] = "ford") {
        document.write(cars[i] + "you are driving a nice ford")
    }
}

The reason why your code is printing the message multiple times is because of this line here

You are using the assignment = operator but you probably meant to use the equality === operator instead

With your current code this condition will always be true because you are assigning the string ford to cars[i]

When you change it to be the equality operator, then the condition will be true only if the current car is a ford

that’s true thanks alot

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.