Building a small game


#1

Hi, I'm new to codecademy and to this forum, yet I've covered only 5 chapters of JS.
I'm trying to build an "8 ball ask question" game, and actually it all works (because it's way too simple).
But anyway, my question is: is it possible to automate (e.g. put in one to several strings of code) the part with "if (eightball < 0.N) {eightball = answers[N];"
Thank you.
Here's my code:

var answers = ["Yes.", "No.", "I'm not sure.", "Sure!", "No way", "I can't tell you.", "You know the answer", "Don't even think of it", "Of course.", "Sorry, try later."];

var eightball = Math.random();
if (eightball < 0.1) {
    eightball = answers[1];
}
else if (eightball < 0.2){
    eightball = answers[2];
}

else if (eightball < 0.3){
    eightball = answers[3];
}
    
else if (eightball < 0.4){
     eightball = answers[4];
}

else if (eightball < 0.5){
     eightball = answers[5];
}

else if (eightball < 0.6){
     eightball = answers[6];
}

else if (eightball < 0.7){
     eightball = answers[7];
}

else if (eightball < 0.8){
     eightball = answers[8];
}

else if (eightball < 0.9){
     eightball = answers[9];
}

else if (eightball <= 1){
     eightball = answers[10];
};


#2

are you asking whether you can output more than one 8-ball answer at a time?


#3

You could do something like

var eightball = Math.floor(Math.random() * 11);
eightball = answers[eightball]

Math.floor() rounds down whatever number is in the parenthesis. Math.Random gives you a random number between 0 < x < 0.999.... So if you multiply Math.Random by 11, you will get something between
0 < x < 10.999... You then floor that number, so you will get something between 0 and 10.