JavaScript实现数组元素展示及用户名(名+姓)显示问题
Complete Solution to Generate Full Usernames
Hey there! Let's finish up your getData function so it generates those full name-surname usernames you need. I'll also fix a couple of small issues in your existing code to make it cleaner and avoid unexpected behavior.
Here's the completed function:
function getData(count) { // Fixed: Use const for non-changing arrays to avoid global scope leaks const names = ["Michał", "Jan", "Katarzyna", "Andrzej", "Józef", "Bartek", "Mikołaj", "Tomasz", "Julian", "Brajan", "Dżesika"]; const surnames = ["Noga", "Kowalski", "Nowak", "Pazura", "Duda", "Komorowski", "Tomczyk", "Józefowicz", "Lechicki", "Goldberg"]; const result = []; for (let i = 0; i < count; i++) { // Get random indices for both name and surname const randomNameIndex = Math.floor(Math.random() * names.length); const randomSurnameIndex = Math.floor(Math.random() * surnames.length); // Combine name and surname into a full username const fullUsername = `${names[randomNameIndex]} ${surnames[randomSurnameIndex]}`; // Add to result array result.push(fullUsername); } // Return the array of usernames return result; }
Key improvements and additions:
- Fixed variable scope: Your original
namesarray was declared withoutvar,let, orconst, which would make it a global variable (bad practice!). I changed all array declarations toconstsince they don't need to be modified. - Added random surname selection: We generate a random index for the surnames array just like we do for names.
- Combined into full usernames: Using template literals (
${}) makes it easy to concatenate the name and surname with a space. - Return the result: The function now returns the array of generated usernames so you can use them wherever you need to display them.
Example usage:
// Generate 5 random usernames const usernames = getData(5); console.log(usernames); // Might output: ["Katarzyna Nowak", "Brajan Goldberg", "Jan Kowalski", "Michał Pazura", "Dżesika Tomczyk"]
Optional: Avoid duplicate username pairs
If you want to make sure you don't get the exact same name-surname combination multiple times, you can add a check to track used pairs:
function getData(count) { const names = ["Michał", "Jan", "Katarzyna", "Andrzej", "Józef", "Bartek", "Mikołaj", "Tomasz", "Julian", "Brajan", "Dżesika"]; const surnames = ["Noga", "Kowalski", "Nowak", "Pazura", "Duda", "Komorowski", "Tomczyk", "Józefowicz", "Lechicki", "Goldberg"]; const result = []; const usedPairs = new Set(); while (result.length < count) { const randomNameIndex = Math.floor(Math.random() * names.length); const randomSurnameIndex = Math.floor(Math.random() * surnames.length); const fullUsername = `${names[randomNameIndex]} ${surnames[randomSurnameIndex]}`; if (!usedPairs.has(fullUsername)) { usedPairs.add(fullUsername); result.push(fullUsername); } } return result; }
This uses a Set to keep track of pairs we've already added, so we only push unique combinations to the result.
内容的提问来源于stack exchange,提问作者mikitheboy
相关产品推荐
相关产品推荐

