Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
Can I compare the answer of a prompt to properties of objects in an array?

I am trying to match the result of a prompt with properties of various objects in an array. (I.e if city entered matches the name of one of the objects listed, return the coinciding temp)

let cities = [
  {
    name: "San Luis Obispo",
    temp: 16,
    uv: 1,
    humidity: 5,
    elevation: 28,
    wind: 2,
  },
  {
    name: "Morro Bay",
    temp: 11,
    uv: 2,
    humidity: 8,
    elevation: 1,
    wind: 8,
  }]

let city = prompt("Enter a city");
if (city === cities.name){
  alert(`It is currently ${temp}°C in ${city}`);
} 
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

let cities = [
  {
    name: "San Luis Obispo",
    temp: 16,
    uv: 1,
    humidity: 5,
    elevation: 28,
    wind: 2,
  },
  {
    name: "Morro Bay",
    temp: 11,
    uv: 2,
    humidity: 8,
    elevation: 1,
    wind: 8,
  }]

let cityName = prompt("Enter a city");
const city = cities.find(cityX => {
    return cityName === cityX.name
})
if (city){
  alert(`It is currently ${city.temp}°C in ${cityName}`);
} 
about 4 years ago · Juan Pablo Isaza Relatório

0

Try this

let city = prompt("Enter a city");
Cities.forEach(current=>{
if (city.trim() === current.name){
  alert(`It is currently ${current.temp}°C in ${city}`);
} 
})
about 4 years ago · Juan Pablo Isaza Relatório

0

run a for loop over the array of objects ( cities ), checking each city in the array:

let cities = [{
    name: "San Luis Obispo",
    temp: 16,
    uv: 1,
    humidity: 5,
    elevation: 28,
    wind: 2,
},
{
    name: "Morro Bay",
    temp: 11,
    uv: 2,
    humidity: 8,
    elevation: 1,
    wind: 8,
}
]

let city = prompt("Enter a city");
for (let i = 0; i < cities.length; i++) {
    if (city == cities[i].name) {
        alert(`It is currently ${cities[i].temp}°C in ${city}`);
    }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda