对于学校我正在做一项任务,我们需要为男性和女性名字制作一个随机名称生成器,我们必须在它前面加上数字,但我无法弄清楚如何做到这一点。发电机本身正在工作但它的数字部分我无法弄清楚。数字需要从1到最大值100。
var number = ["1", "2", "3", "4", "5"];
var randomnumber = Math.floor(Math.random() * number.length);
var counter = number[randomnumber];
document.write("Female Names");
document.write("<br>");
function femaleName (){
// My variables.
// Klinkers.
var klinker = ["o", "a", "i", "e", "u"];
var randomnumberk = Math.floor(Math.random() * klinker.length);
var randomklinker = klinker[randomnumberk];
var randomnumberk1 = Math.floor(Math.random() * klinker.length);
var randomklinker1 = klinker[randomnumberk1];
var randomnumberk2 = Math.floor(Math.random() * klinker.length);
var randomklinker2 = klinker[randomnumberk2];
var randomnumberk3 = Math.floor(Math.random() * klinker.length);
var randomklinker3 = klinker[randomnumberk3];
var randomnumberk4 = Math.floor(Math.random() * klinker.length);
var randomklinker4 = klinker[randomnumberk4];
// Medeklinkers.
var medeklinker = ["b", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "v", "w", "z",]
var randomnumbermk = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker = medeklinker[randomnumbermk];
var randomnumbermk1 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker1 = medeklinker[randomnumbermk1];
var randomnumbermk2 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker2 = medeklinker[randomnumbermk2];
var randomnumbermk3 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker3 = medeklinker[randomnumbermk3];
var randomnumbermk4 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker4 = medeklinker[randomnumbermk4];
document.write(number, randomklinker, randommedeklinker, randomklinker1, randommedeklinker1, randomklinker2);
document.write("<br>");
}
for ( var i = 0; i < 6; i++) {
femaleName();
}
document.write("<br>");
document.write("<br>");
document.write("<br>");
document.write("Male names");
document.write("<br>");
function maleName (){
// My variables.
// Klinkers.
var klinker = ["o", "a", "i", "e", "u"];
var randomnumberk = Math.floor(Math.random() * klinker.length);
var randomklinker = klinker[randomnumberk];
var randomnumberk1 = Math.floor(Math.random() * klinker.length);
var randomklinker1 = klinker[randomnumberk1];
var randomklinker2 = klinker[randomnumberk2];
var randomnumberk3 = Math.floor(Math.random() * klinker.length);
var randomklinker3 = klinker[randomnumberk3];
var randomnumberk4 = Math.floor(Math.random() * klinker.length);
var randomklinker4 = klinker[randomnumberk4];
// Medeklinkers.
var medeklinker = ["b", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "v", "w", "z",]
var randomnumbermk = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker = medeklinker[randomnumbermk];
var randomnumbermk1 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker1 = medeklinker[randomnumbermk1];
var randomnumbermk2 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker2 = medeklinker[randomnumbermk2];
var randomnumbermk3 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker3 = medeklinker[randomnumbermk3];
var randomnumbermk4 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker4 = medeklinker[randomnumbermk4];
document.write(randommedeklinker, randomklinker, randommedeklinker1, randomklinker1, randommedeklinker2);
document.write("<br>");
}
for ( var i = 0; i < 6; i++) {
maleName();
}
我希望有人可以帮我解决这个问题。
答案 0 :(得分:1)
这些问题总让我怀疑最好的答案是什么。显然,RRZ提供的解决方案有效,应该标记为答案。 然而,我相信如果您确保您的代码是以简明扼要的方式编写的,那么您自己会发现它!
我不想为你做功课,但我想向你展示另一种编写这个程序的方法。
为了使这更加切实,我已经以一种能够回答这些子弹的方式实现了您的代码:
// Settings
var NR_OF_NAMES = 6,
// K = Klinker, M = Medeklinker
FEMALE_NAME_PATTERN = "KMKMK",
MALE_NAME_PATTERN = "MKMKM";
function getRandomIntBelow(max) {
return Math.floor(Math.random() * max);
};
function getRandomFromArray(array) {
return array[getRandomIntBelow(array.length)];
}
function getRandomKlinker() {
return getRandomFromArray(["o", "a", "i", "e", "u"]);
}
function getRandomMedeklinker() {
return getRandomFromArray(["b", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "v", "w", "z"]);
}
function getNameByPattern(pattern) {
return pattern
.split("") // Array of characters
.map(function(code) {
// Replace M by medeklinker
if (code === "M") return getRandomMedeklinker();
// Replace K by klinker
if (code === "K") return getRandomKlinker();
})
.join(""); // Back to string
}
// Document creation:
// Render a list of names
function writeNameBlock(pattern, count) {
for (var i = 0; i < count; i++) {
document.write("<br>");
document.write(i + 1 + ". ");
document.write(getNameByPattern(pattern));
}
}
// Render the outlinse and include two lists
document.write("Female Names");
writeNameBlock(FEMALE_NAME_PATTERN, NR_OF_NAMES);
document.write("<br>");
document.write("<br>");
document.write("Male names");
writeNameBlock(MALE_NAME_PATTERN, NR_OF_NAMES);
&#13;
答案 1 :(得分:0)
代码需要改进。现在,这是一个解决方案......
document.write(数字,randomklinker,randommedeklinker ...);
将号码更改为(i + 1)
答案 2 :(得分:0)
我并不完全清楚你想要什么,但如果要在每个名字之前添加序列号,你只需要将变量声明为i + 1
(加上任何格式,如后面的空格)。 + 1
是因为i
为零索引。希望这可以帮助。随着循环的每次迭代,序列号递增。
var number = ["1", "2", "3", "4", "5"];
var randomnumber = Math.floor(Math.random() * number.length);
var counter = number[randomnumber];
document.write("Female Names");
document.write("<br>");
function femaleName (){
var sequenceNumber = i + 1 + ' ';
// My variables.
// Klinkers.
var klinker = ["o", "a", "i", "e", "u"];
var randomnumberk = Math.floor(Math.random() * klinker.length);
var randomklinker = klinker[randomnumberk];
var randomnumberk1 = Math.floor(Math.random() * klinker.length);
var randomklinker1 = klinker[randomnumberk1];
var randomnumberk2 = Math.floor(Math.random() * klinker.length);
var randomklinker2 = klinker[randomnumberk2];
var randomnumberk3 = Math.floor(Math.random() * klinker.length);
var randomklinker3 = klinker[randomnumberk3];
var randomnumberk4 = Math.floor(Math.random() * klinker.length);
var randomklinker4 = klinker[randomnumberk4];
// Medeklinkers.
var medeklinker = ["b", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "v", "w", "z",]
var randomnumbermk = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker = medeklinker[randomnumbermk];
var randomnumbermk1 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker1 = medeklinker[randomnumbermk1];
var randomnumbermk2 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker2 = medeklinker[randomnumbermk2];
var randomnumbermk3 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker3 = medeklinker[randomnumbermk3];
var randomnumbermk4 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker4 = medeklinker[randomnumbermk4];
document.write(sequenceNumber, randomklinker, randommedeklinker, randomklinker1, randommedeklinker1, randomklinker2);
document.write("<br>");
}
for ( var i = 0; i < 6; i++) {
femaleName();
}
document.write("<br>");
document.write("<br>");
document.write("<br>");
document.write("Male names");
document.write("<br>");
function maleName (){
var sequenceNumber = i + 1 + ' ';
// My variables.
// Klinkers.
var klinker = ["o", "a", "i", "e", "u"];
var randomnumberk = Math.floor(Math.random() * klinker.length);
var randomklinker = klinker[randomnumberk];
var randomnumberk1 = Math.floor(Math.random() * klinker.length);
var randomklinker1 = klinker[randomnumberk1];
var randomnumberk2 = Math.floor(Math.random() * klinker.length);
var randomklinker2 = klinker[randomnumberk2];
var randomnumberk3 = Math.floor(Math.random() * klinker.length);
var randomklinker3 = klinker[randomnumberk3];
var randomnumberk4 = Math.floor(Math.random() * klinker.length);
var randomklinker4 = klinker[randomnumberk4];
// Medeklinkers.
var medeklinker = ["b", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "v", "w", "z",]
var randomnumbermk = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker = medeklinker[randomnumbermk];
var randomnumbermk1 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker1 = medeklinker[randomnumbermk1];
var randomnumbermk2 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker2 = medeklinker[randomnumbermk2];
var randomnumbermk3 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker3 = medeklinker[randomnumbermk3];
var randomnumbermk4 = Math.floor(Math.random() * medeklinker.length);
var randommedeklinker4 = medeklinker[randomnumbermk4];
document.write(sequenceNumber, randommedeklinker, randomklinker, randommedeklinker1, randomklinker1, randommedeklinker2);
document.write("<br>");
}
for ( var i = 0; i < 6; i++) {
maleName();
}