So I've been looking on how to make a simple coin flip script using javascript and I see these videos of people that seem to over-complicate things using 20 different elements or whatever you call them. So what I set out to do is create a "magic 8 ball" kind of script but using only things that a super noob such as myself would know, instead of trying to memorize so many different things. So I have it working so that I have a text box, then next to it is a button when pressed it will execute a function. The problem I'm having is instead of returning a number when using Math.floor(Math.random() * 4) I want to return the words like "yes" "no" "maybe" ect. What I'm wondering, is what the () do in Math.random. Anyway, instead of trying to explain what I did here is my javascript using the most simple statements I could. The first statement works if nothing is typed in it says "please enter a question" but the rest doesn't.
function checkFunction() {
if (question.value == '') {
document.getElementById("fortune").innerHTML = "Please Enter A Question";
} else {
Math.floor(Math.random(data) * 4);
if (data == 0) {
document.getElementById("fortune").innerHTML = "Maybe";
} else if (data == 1) {
document.getElementById("fortune").innerHTML = "Try Again";
} else if (data == 2) {
document.getElementById("fortune").innerHTML = "Yes";
} else if (data == 3) {
document.getElementById("fortune").innerHTML = "No";
} else {
return "Invalid";
}
}
}
I don't care if its long, I just want it to be simple. I don't want to have to define a bunch of variables and mess around with calling them up because I'm just trying to take it a step at a time. I'm basically trying to figure out the maximum potential of an if statement before moving on and learning new things.
Aucun commentaire:
Enregistrer un commentaire