Pablo Ivan Pablo Ivan - 4 months ago 8
Javascript Question

Create a prediction game on Javascript

Is there a way to improve my basic "answering" program that returns a random answer on Javascript?

Something like:

var question = prompt("Ask something: ");
var randomizer = Math.random();
var answer;

if (randomizer > 0.1 && randomizer < 0.5) {
answer = alert ("Yes, it will.");
} else {
answer = alert ("That won't happen.");
}

Answer

Just add some brackets.

var question = prompt("Ask something: ", '');
var randomizer = Math.random;
if (randomizer() < 0.5) { // call function randomizer with ()
    alert(question + " Yes, it will.");
} else {
    alert(question + " That won't happen.");
}

Comments