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

205
Visualizações
JavaScript user prompt 2d array lookup - Elf Name Generator

Started with what I thought was a simple idea for a class activity for our JavaScript unit, but falling foul best way to read a JavaScript 2D array based on a user input.

How can I get it to look up the users entry in the first index of the array and output the value in the second index? I need to tell it at some point which index to read and which to output, via similar syntax to [0][1] I imagine, but trial and error is getting me far.

Perhaps there is a better way??

Here is what I have so far, which just outputs the first array entry in its entirety - "A","Angelic"

var firstNames = [
["A","Angelic"],
["B","Blustery"],
........
["Y","Cheery"],
["Z","Dancy"]
];
var firstInitial = window.prompt("What is the first letter of your first name: ").toUpperCase();
let elfFirstName = firstNames.find(firstSearch);
function firstSearch(firstInitial) {
    return firstInitial;
}
window.alert("Your Elf Name is " + elfFirstName);
</script>```


Thanks
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You do not use find right. It needs to look at the first index of your array that is passed in.

function firstSearch(item) {
  return item[0] === firstInitial;
}

So

var firstNames = [
  ["A", "Angelic"],
  ["B", "Blustery"],
  ["Y", "Cheery"],
  ["Z", "Dancy"]
];
var firstInitial = window.prompt("What is the first letter of your first name: ").toUpperCase();
const elfFirstName = firstNames.find(firstSearch)[1];

function firstSearch(item) {
  return item[0] === firstInitial;
}
window.alert("Your Elf Name is " + elfFirstName);

Better solution as I pointed out in the comments is using an array

var firstNames = {
  A: "Angelic",
  B: "Blustery",
  Y: "Cheery",
  Z: "Dancy"
};
var firstInitial = window.prompt("What is the first letter of your first name: ").toUpperCase();
const elfFirstName = firstNames[firstInitial];
window.alert("Your Elf Name is " + elfFirstName);

about 4 years ago · Juan Pablo Isaza Relatório

0

You could take an object with the initials as keys.

const
    firstNames = {
        A: "Angelic",
        B: "Blustery",
        Y: "Cheery",
        Z: "Dancy"
    },
    firstInitial = window.prompt("What is the first letter of your first name: ")[0].toUpperCase(),
    elfFirstName = firstNames[firstInitial];

console.log("Your Elf Name is " + elfFirstName);

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