-1

I'm trying to write a function that checks whether a value is odd or even, but it isn't quite working as it should, or rather, at all.

The code I got was from this question.

        function isEven(value) {
            if (value % 2 == 0) {
                return true;
                console.log("True");
            }
            else {
                return false;
                console.log("False");
            }
            console.log(value);
        }

        isEven(3);

I get no errors in the browser's console, but it also does not log True, False, or value.

Note: I will also accept jQuery solutions, but would prefer Javascript.

Community
  • 1
  • 1
  • you are returning before loging, just change the order – bto.rdz Jul 03 '15 at 03:56
  • you have to remove the line with the `return` –  Jul 03 '15 at 03:57
  • 1
    also once you are convinced this is the behavior that you want. Instead of returning bool constants return what you are checking. e.g. `function isEven (value) { return value % 2 === 0; }` – t3dodson Jul 03 '15 at 03:57

2 Answers2

2

You are returning before logging. Anything after return will not be executed.

return after logging

 function isEven(value) {
   if (value % 2 == 0) {
     console.log("True");
     return true;
   } else {
     console.log("False");
     return false;
   }
 }

 isEven(3);
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
0

Ensure value is an integer first by parsing it with

parseInt(value)
omikes
  • 8,064
  • 8
  • 37
  • 50