I need to figure out how to make a random number generator from 1-100 using HTML javascript. After finding the number from 1-100 I need to tell whether the number is odd or even. This is what I have so far:
<!DOCTYPE html>
<html>
<body>
<p>By clicking this button you will genarte a number from 1 to 100 </p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("demo")
x.innerHTML = Math.floor((Math.random() * 100) + 1);
if (a%==0)
system.out.println("Even Number");
else
system.out.println("Odd Number");
}
</script>
</body>
</html>
I think I have the random number generator right, but not the odd or even. Please help.
I need to figure out how to make a random number generator from 1-100 using HTML javascript. After finding the number from 1-100 I need to tell whether the number is odd or even. This is what I have so far:
<!DOCTYPE html>
<html>
<body>
<p>By clicking this button you will genarte a number from 1 to 100 </p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("demo")
x.innerHTML = Math.floor((Math.random() * 100) + 1);
if (a%==0)
system.out.println("Even Number");
else
system.out.println("Odd Number");
}
</script>
</body>
</html>
I think I have the random number generator right, but not the odd or even. Please help.
%
operator is, and how to use it, then change a%==0
change to a%2==0
- also, Int
? this is not JAVA, this is javascript (you also tagged the question java!?) hint: browsers have a developer tools console - you'll see errors in it with the code you've written
– Jaromanda X
Commented
Nov 21, 2017 at 23:41
You seem to have mixed up Java and JavaScript a little bit. JavaScript is what runs in browser, Java is a VM you download (and can sometimes run in browser, if you have the extension enabled/installed). system.out.println
is Java. I went ahead and changed stuff out to make it entirely JavaScript.
function myFunction() {
var x = document.getElementById("demo"),
randomNum = Math.floor((Math.random() * 100) + 1);
x.innerHTML = randomNum
if (randomNum % 2 == 0) {
console.log("Even Number");
} else {
console.log("Odd Number");
}
}
<!DOCTYPE html>
<html>
<body>
<p>By clicking this button you will genarte a number from 1 to 100 </p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
You seem to be mixing java / javascript, to show you the code first:
<!DOCTYPE html>
<html>
<body>
<p>By clicking this button you will genarte a number from 1 to 100 </p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var ele = document.getElementById("demo");
var number = Math.floor((Math.random() * 100) + 1);
ele.innerHTML = number
if (number%2==0) {
alert("Even Number");
}
else {
alert("Odd Number");
}
}
</script>
</body>
</html>
There is no system.out.println
in Javascript, you needed some more brackets and lastly to check for even check you missed a %2
.