javascript - Random num Generator odd and even - Stack Overflow

admin2025-04-20  0

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.

Share Improve this question edited Nov 21, 2017 at 23:44 Brandit28 asked Nov 21, 2017 at 23:40 Brandit28Brandit28 111 silver badge5 bronze badges 3
  • 1 read what % 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
  • Java and JavaScript are different languages. – Neil Commented Nov 21, 2017 at 23:43
  • This is half java half javascript. It's not gonna work like that. – James Commented Nov 21, 2017 at 23:44
Add a ment  | 

2 Answers 2

Reset to default 3

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745081357a283877.html

最新回复(0)