I'm trying to complete exercise 2.7 of the FSopen class
I am trying to create a simple phonebook, which can store an array of 'person' objects that have a 'name' attribute. Here is how the data is structured:
persons = { name: 'Arto Hellas'}
When the user enters the name (that they want to add to the phonebook) into the form input field and presses submit, it triggers an event that first checks if that name already exists in the contact list. Here is the code for that:
const addPerson = (event) => {
event.preventDefault()
const personObj = {name: newName}
const exists = persons.find(n => n.name === newName)
{(exists === newName) ? alert('name already exists') : setPersons(persons.concat(personObj))}
setNewName('')
}
My issue is that the (exists === newName) statement always returns false, even when the two variables are identical. Resulting in the person being added to the contact list even if a copy already exists. I'm not sure what the issue is here. Any help would be appreciated.