-1

My Javascript code (in an HTML document) returns no output. Without the if/else, it functions, but even a simple if does not work.

Goal: Find out HTML input value's modulus, and return even if 0 and odd if anything else.

Code(s):

HTML Input + Button:

<input id="num" type="number" min="1" name="num">
      <button onclick="calculate(); evenodd();  return false;">Go</button>

(The calculate() is another JS function, which works perfectly)

HTML Output:

<p id="evod"></p>

Javascript:

<script type="text/javascript">

      function evenodd () {


        var num2 = parseInt(document.getElementById("num").value);


        if (num2 % 2) == 0) {
          document.getElementById("evod").innerHTML = "Even"; 
            }

        else {
          document.getElementById("evod").innerHTML = "Odd";
            }

      }

     </script type="text/javascript">
     </script>
5
  • does evenodd() even run? Try a console.log maybe? You've got another function there called calculate(), and if that doesn't exist (which, according to the code you provided, it doesn't), I don't think the next function would execute.
    – sg.cc
    Oct 7, 2015 at 15:17
  • 1
    Keep your browser developer console open. The first thing to do when your JavaScript code doesn't work is check for errors in the console!
    – Pointy
    Oct 7, 2015 at 15:20
  • You've tagged this with jquery but then not used even the most basic jquery - please remove the tag or confirm it's ok to be removed.
    – freedomn-m
    Oct 7, 2015 at 15:24
  • You're parsing your input in base 8 - I assume this is unintended... developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – freedomn-m
    Oct 7, 2015 at 15:25
  • @sg.cc calculate() exists, just that I did not provide it.
    – user5413945
    Oct 7, 2015 at 17:00

1 Answer 1

4

You've got a syntax error:

if (num2 % 2) == 0) {

You're missing the opening parenthesis immediately before num2 as so:

if ((num2 % 2) == 0) {

Also, you have malformed HTML. You're script should be formatted as such:

<script type="text/javascript">
// JS code
</script>

You should never have attributes in a closing tag.

To debug this yourself, try opening the developer console in your browser (Ctrl+Shift+J or F12 will get you there in most cases) and see what errors are being reported.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.