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

204
Views
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 answers
Answer question

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 Report

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 Report
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!