Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    correct text for displaying remainder

    Good evening. I'm very new to this, but very excited that I've gotten this code so close. I am trying to get the computer to determine if a number is even or not. This is what I have:

    [CODE]
    <script type = "text/javascript">
    <!--
    var firstNumber; // first string entered by user
    var answer; // the division problem

    // read in first number from user as a string
    firstNumber = window.prompt("Enter an integer");

    //how to get a remainder
    var answer = firstNumber / 2

    if (answer > 1)
    {
    document.writeln(answer + ". This number is even.");
    }

    </script>

    The bottom part is the problem. I am able to code that if the answer is greater than one, then return the following text, however, I can't for the life of me figure how to say... if the remainder is zero.

    Thank you,

  • #2
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    You'll probably find it easier if you use the modulus operator (%) which returns the actual remainder of a division. If the remainder after dividing by 2 is 0 then the numerator is even, otherwise the numerator is an odd number.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
            <title></title>
            <style type="text/css"></style>
            <script type="text/javascript">
                function checkNumber(){
                    var num = Number(document.getElementById('txtInp').value);
                    if(num%2 == 0){ //num is even
                        alert('even');
                    }else{ //num is odd
                        alert('odd');
                    }
                    document.getElementById('txtInp').value = '';
                }
            </script>
        </head>
        <body>
            <div>
                <input type="text" id="txtInp" />
                <button onclick="checkNumber();">Check number</button>
            </div>
        </body>
    </html>

  • #3
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    ty. after pounding my head a bit more, i also changed the if then statement to

    if (answer%2=0)
    {
    document.writeln(answer + ". This number is even.");
    }

    and that works in the way i want, but it also identifies odd numbers as odd which i don't have in my code. why?

  • #4
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by hmcka View Post
    that works in the way i want, but it also identifies odd numbers as odd which i don't have in my code. why?
    I'm not sure without seeing all of your updated code.

    The demo I posted displays either odd or even in the alert() for each number entered in the textbox.

    Also, wrap your code in code tags by first highligting your code and then clicking the # icon in the editors toolbar. It'll make your code easier to read.
    Last edited by webdev1958; 03-06-2012 at 02:23 AM.

  • #5
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    arghh. i figured why. nevermind. thank you for your reply.

  • #6
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    glad it's sorted

    btw,

    Code:
    if (answer%2=0)
    should be

    Code:
    if (answer%2 == 0)
    = means assign a value to something like var myNum = 123

    == is a comparison operator to check if 2 variables are equal to each other.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,456
    Thanks
    0
    Thanked 632 Times in 622 Posts
    If you put the number first and the variable second in comparisons and use identically equal comparisons then leaving out an = will be much easier to spot.

    Code:
    if (0 === answer%2)
    leave out one = and it will still work, leave out two = and it will fail rather than doing the wrong thing
    Last edited by felgall; 03-06-2012 at 05:38 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    nup, personally I don't like doing it the other way round. Prefer to just get it right the first time and it reads better for me the original way


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •