我无法使我的功能正常工作。
这是我的代码:
function createPlayer() {
var nation = new Array("Germany", "USA", "France", "Spain", "Belgium", "Nigeria", "Camerun", "Egypt", "Morocco", "SouthAfrica");
var deFN = new Array("Andreas", "Daniel", "Kevin", "Lukas", "Timo", "Lennard");
var usFN = new Array("Tyler", "Ron");
var frFN = new Array("Lucas", "Mathis");
var deLN = new Array("Müller", "Meyer", "Ries", "Kellermann", "Werner", "König");
var usLN = new Array("Stinson", "Wellington", "Booker", "James", "Owen", "Nolan");
var frLN = new Array("Lefevre", "Bonnet");
var nationPlayer = nation[Math.floor(Math.random() * nation.length)];
var namePlayer = new Array("");
if (nationPlayer == Germany) {
var namePlayer = deFN[Math.floor(Math.random() * deFN.length)] + deLN[Math.floor(Math.random() * deLN.length)];
} else if (nationPlayer == USA) {
var namePlayer = usFN[Math.floor(Math.random() * usFN.length)] + usLN[Math.floor(Math.random() * usLN.length)];
} else if (nationPlayer == France) {
var namePlayer = frFN[Math.floor(Math.random() * frFN.length)] + frLN[Math.floor(Math.random() * frLN.length)];
} else {
var namePlayer = "Other nation"
}
console.log(namePlayer);
}
首先,这个函数应该选择一个民族数组的随机国家。 然后,如果随机选择器选择了这三个国家中的一个,它应该将国家,名字和姓氏连接到变量Array" namePlayer"
(抱歉,我是新手)
答案 0 :(得分:1)
您需要将nationPlayer == Germany
替换为nationPlayer == "Germany"
,因为未定义变量德语。
function createPlayer() {
var nation = new Array("Germany","USA","France","Spain","Belgium","Nigeria","Camerun","Egypt","Morocco","SouthAfrica");
var deFN = new Array("Andreas","Daniel","Kevin","Lukas","Timo","Lennard");
var usFN = new Array("Tyler", "Ron");
var frFN = new Array("Lucas" , "Mathis");
var deLN = new Array("Müller","Meyer","Ries","Kellermann","Werner","König");
var usLN = new Array("Stinson","Wellington","Booker","James","Owen","Nolan");
var frLN = new Array("Lefevre", "Bonnet");
var nationPlayer = nation[Math.floor(Math.random() * nation.length)];
var namePlayer;
if (nationPlayer == "Germany") {
namePlayer = deFN[Math.floor(Math.random() * deFN.length)] + deLN[Math.floor(Math.random() * deLN.length)];
} else if (nationPlayer == "USA") {
namePlayer = usFN[Math.floor(Math.random() * usFN.length)] + usLN[Math.floor(Math.random() * usLN.length)];
} else if (nationPlayer == "France") {
namePlayer = frFN[Math.floor(Math.random() * frFN.length)] + frLN[Math.floor(Math.random() * frLN.length)];
} else {
namePlayer = "Other nation"
}
console.log(namePlayer);
}
createPlayer()