Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
Why comparison of array values only matches most recently entered object value?

In the snippet below, if I enter a value in the textbox, it's supposed to return something. i.e.; entering 'PA' returns 'Pennsylvania'. But if I enter 'AZ', it returns my else statement.

How do I change this so that it checks the value against any value entered and displays the result properly?

const states = [
  {
    abbreviated: 'AZ',
    state: 'Arizona'
  }, {
    abbreviated: 'PA',
    state: 'Pennsylvania'
  }
];

function searchState() {
  let txt = searchTxt.value;
  for (var i = 0; i < states.length; i++) {
    var abbreviatedState = states[i];
    if (txt == abbreviatedState.abbreviated) {
      output.textContent = abbreviatedState.state;
    } else {
      output.textContent = 'Uh, oh! We don\'t know that one!';
    }
  }
}

document.querySelector('.search').addEventListener("click", ()=>searchState());
<input name="searchTxt" type="text" id="searchTxt" class="searchField" placeholder="State Abbreviation?"/>

<button class="search">Search</button>

<h4>State Of: <span id="output"></span></h4>

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Run the loop to the end, and only assign Uh oh if nothing is found. To make the logic easier, use .find instead of a for loop.

const states = [
  {
    abbreviated: 'AZ',
    state: 'Arizona'
  }, {
    abbreviated: 'PA',
    state: 'Pennsylvania'
  }
];

function searchState() {
  const txt = searchTxt.value;
  const foundState = states.find(state => state.abbreviated === txt);
  output.textContent = foundState
    ? foundState.state
    : 'Uh, oh! We don\'t know that one!'
}

document.querySelector('.search').addEventListener("click", ()=>searchState());
<input name="searchTxt" type="text" id="searchTxt" class="searchField" placeholder="State Abbreviation?"/>

<button class="search">Search</button>

<h4>State Of: <span id="output"></span></h4>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your code goes into the else block even if it goes into if once. I've refactored the code to set "uh oh" as the default value, only searches for if and breaks out once found.

const states = [{
  abbreviated: 'AZ',
  state: 'Arizona'
}, {
  abbreviated: 'PA',
  state: 'Pennsylvania'
}];

function searchState() {
  let txt = searchTxt.value;
  output.textContent = 'Uh, oh! We don\'t know that one!';
  for (var i = 0; i < states.length; i++) {
    var abbreviatedState = states[i];
    if (txt == abbreviatedState.abbreviated) {
      output.textContent = abbreviatedState.state;
      break;
    }
  }
}

document.querySelector('.search').addEventListener("click", () => searchState());
<input name="searchTxt" type="text" id="searchTxt" class="searchField" placeholder="State Abbreviation?" />

<button class="search">Search</button>

<h4>State Of: <span id="output"></span></h4>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Because you continue searching event after AZ state is found and it goes into else later.

const states = [{
  abbreviated: 'AZ',
  state: 'Arizona'
}, {
  abbreviated: 'PA',
  state: 'Pennsylvania'
}];

function searchState() {
  let txt = searchTxt.value;
  console.log(txt);
  const output = document.querySelector('#output');
  for (var i = 0; i < states.length; i++) {
    var abbreviatedState = states[i];
    if (txt == abbreviatedState.abbreviated) {
      output.textContent = abbreviatedState.state;
      break;
    } else {
      output.textContent = 'Uh, oh! We don\'t know that one!';
    }
  }
}

document.querySelector('.search').addEventListener("click", () => searchState());
<input name="searchTxt" type="text" id="searchTxt" class="searchField" placeholder="State Abbreviation?" />

<button class="search">Search</button>

<h4>State Of: <span id="output"></span></h4>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda