我对编码非常陌生(使用 codeacademy 来学习 Javascript),我需要一些关于我正在从事的迷你项目的帮助。我试图让计算机从两个不同的数组中随机选择一个男孩名或女孩名。我使用 if/else 语句让计算机在 boynames 数组或 girlnames 数组之间进行选择。出于某种原因,当我运行代码时,它只执行 girlname 循环(“if”部分)但不运行 boyname 循环(“else”部分。)关于如何让它执行 if/else 的任何建议功能正常?代码如下,提前谢谢!
<script>
var lastname = prompt("What is your last name?");
var useranswer = prompt("Are you having a baby girl or boy");
var girlnames = ["Lana", "Michelle", "Rebecca", "Angelina", "Carrie", "Natalia",
"Rosie", "Heather", "Monica", "Lindsay"]
var random = girlnames[Math.floor(Math.random() * girlnames.length)];
var boynames = ["Justin", "Ryan", "Adler", "Darren", "Michael", "Kyle", "Taylor",
"Winston", "Jacob", "Samuel", "Oliver"]
var rand = boynames[Math.floor(Math.random() * boynames.length)];
if (useranswer = "girl") {
for (var i = 0; i <= girlnames.length; i++) {
confirm("Your future daughter's name is" + " " + random + " " + lastname);
}
} else {
for (var j = 0; j <= boynames.length; j++) {
confirm("Your future son's name is" + " " + rand + " " + lastname);
}
}
</script>