Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In my "if/else" statement my "else" statement isn't working [closed]

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>
like image 544
user2466886 Avatar asked Dec 10 '25 00:12

user2466886


1 Answers

It is because you used = instead of ==.

A single equal sign is used for assignment and two equals are a comparison.

like image 161
TimWolla Avatar answered Dec 11 '25 14:12

TimWolla