I am very new to coding (using codeacademy to learn Javascript) and I need a little help with this mini project I'm working on. I am trying to make the computer randomly choose either a boy name or girl name from two different arrays. I used an if/else statement to make the computer choose between the boynames array or the girlnames array. For some reason when I run the code it only executes the girlname loop (the "if"part) but does not run the boyname loop (the "else" part.) Any advice on how I can get it to execute the if/else function properly? The code is below, thank you in advance!
<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>
It is because you used = instead of ==.
A single equal sign is used for assignment and two equals are a comparison.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With