Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

301
Views
JS React App: array.find not working as expected

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!