My assignment for my programming class is to create a script using JavaScript that asks a user to enter a number in a pop up dialog box on their browser, and then tell them if they entered an odd or an even number. This is what I have come up with so far, whenever I test it on a browser it says that every number is even. I have dug through my textbook looking for answers, but that has not helped much. All I am asking for is a push in the right direction. Thanks.

``````<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!-- Exercise 6.27: 6.27.html -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<title>Exercise 6.27</title>
<script type="text/javascript">
var numberS; //number as a string
var numberI; //number as an integer
var result; //result from dividing integer by 2

//user enters a string
numberS = window.prompt( "Please enter an integer, we will let you know if what you entered is even or odd" );
//convert string into an integer
numberI = parseInt( numberS );
//divide integer by 2
result = numberI / 2;
//if result has no remainder print "This integer is even"
if ( result % 2 )
document.writeln( "This integer is even" );
//else print "This integer is odd"
else
document.writeln( "This integer is odd" );
</script>
</html>``````

## All 7 Replies

Hi,

In the above code the logic is they divided the give number by 2 and again checking the condition by modulo 2 its not correct .
Gust we can check the condition by modulo 2 with the given number as i mentioned below.

//if result has no remainder print "This integer is even"
if (numberI % 2)
document.writeln("This integer is odd");
//else print "This integer is odd"
else
document.writeln("This integer is even");

Try this code it will work.

Thanks :-)

Thanks it worked. I see the logic correctly now, thanks for clearing up my understanding.

I understand it is accepted practice not to, but please learn to use braces { } in your code. trust me. today I am working on the javascript part of a form that is over 1500 lines long. with several 100 - 150 lines of if / else if statements.

``````if ( result % 2 ) {
document.writeln( "This integer is even" );
//else print "This integer is odd"
} else {
document.writeln( "This integer is odd" );
}``````

and kalpanak... is right, you need to use mod on the number input, do not divide by 2, prior to this step.

``````<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!-- Exercise 6.27: 6.27.html -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<title>Exercise 6.27</title>
<script type="text/javascript">
var numberS; //number as a string
var numberI; //number as an integer
var result; //result from dividing integer by 2
//user enters a string
numberS = window.prompt( "Please enter an integer, we will let you know if what you entered is even or odd" );
//convert string into an integer
numberI = parseInt( numberS );

//if result has no remainder print "This integer is even"
// do mod on the input number, do not divide by 2 previously to this.
if ( numberI % 2 ) {
document.writeln( "This integer is odd" );
//else print "This integer is odd"
} else {
document.writeln( "This integer is even" );
}
</script>
</html>``````