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 9 of 9
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Why does my program hang on this line?

    Hey guys,

    I am trying to code a calculator. For some reason my script hangs on the line: equation.push(pressed) inside of the Check(pressed) function. Can anyone provide any insight?

    Code:
    <html>
    <head>
    <script>
            var equation = new Array();
            var num1;
            var num2;
            var operation;
            var total;
            function Check(pressed){
              if (pressed=="="){
                      document.write("Test 0");
                      num2=equation.join('');
                      total=Calculate(num1,num2,operation);
                      document.write('The total is: ' + total);
                      }
              else if (!isNaN(pressed)){
                      document.write("Test 1");
                      equation.push(pressed);
                      Display(pressed);
                      }
              else{
                      operation = pressed;
                      num1=equation.join('');
                      var equation = new Array();
                      }
            }
            function Display(update){
              document.write("Display runs");
              document.getElementById("main").innerHTML+=update;
            }
            function Calculate(num1,num2,operation){
              if (operation=="/"){
                      total=num1/num2;
                      return total;
                      }
              else if (operation=="*"){
                      total=num1*num2;
                      return total;
                      }
              else if (operation=="-"){
                      total=num1-num2;
                      return total;
                      }
              else{
                      total=num1+num2;
                      return total;
                      }
            }
    </script>
    <link rel="stylesheet" href="calcstyle.css" type="text/css">
    </head>
    <body>
            <p class="screen" id="main"></p>
            <button type="button" class="style" onclick="Check('1')">1</button>
            <button type="button" class="style" onclick="Check('2')">2</button>
            <button type="button" class="style" onclick="Check('3')">3</button>
            <button type="button" class="style" onclick="Check('/')">/</button>
            <br>
            <button type="button" class="style" onclick="Check('4')">4</button>
            <button type="button" class="style" onclick="Check('5')">5</button>
            <button type="button" class="style" onclick="Check('6')">6</button>
            <button type="button" class="style" onclick="Check('*')">*</button>
            <br>
            <button type="button" class="style" onclick="Check('7')">7</button>
            <button type="button" class="style" onclick="Check('8')">8</button>
            <button type="button" class="style" onclick="Check('9')">9</button>
            <button type="button" class="style" onclick="Check('-')">-</button>
            <br>
            <button type="button" class="style" onclick="Check('.')">.</button>
            <button type="button" class="style" onclick="Check('0')">0</button>
            <button type="button" class="style" onclick="Check('=')">=</button>
            <button type="button" class="style" onclick="Check('+')">+</button>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,762
    Thanks
    23
    Thanked 548 Times in 547 Posts
    When you use document.write() you wipe everything clean. Don't use it after the page is written!

    If your checking stuff use an alert().
    Evolution - The non-random survival of random variants.

  • #3
    New Coder
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    When you use document.write() you wipe everything clean. Don't use it after the page is written!

    If your checking stuff use an alert().
    Yes I know it wipes it, I am just seeing which functions run to test it. Either way though, the problem still persists, so how to fix it?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I don't think you understood what he said.

    When you use document.write() it wipes out YOUR ENTIRE HTML PAGE, *INCLUDING* all of your JavaScript, even the JavaScript that *DID* the document.write.

    DO NOT USE document.write

    PERIOD.

    As he said, replace document.write() with alert( ).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    I don't think you understood what he said.

    When you use document.write() it wipes out YOUR ENTIRE HTML PAGE, *INCLUDING* all of your JavaScript, even the JavaScript that *DID* the document.write.

    DO NOT USE document.write

    PERIOD.

    As he said, replace document.write() with alert( ).
    I understand what he said. But what I am saying is if the function runs, it will display all of the document.writes in the order that the functions run. I don't care about this matter though, I just want to know why it hangs at equation.push(pressed).

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,762
    Thanks
    23
    Thanked 548 Times in 547 Posts
    When you reused your new array you caused problems
    Code:
              else{
                      operation = pressed;
                      num1=equation.join('');
                      //var equation = new Array(); THIS HAS BEEN DONE  IF YOU WANT TO EMPTY IT THERE IS A COMMAND TO DO THAT equation.length = 0;
                      }
    Evolution - The non-random survival of random variants.

  • #7
    New Coder
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    When you reused your new array you caused problems
    Code:
              else{
                      operation = pressed;
                      num1=equation.join('');
                      //var equation = new Array(); THIS HAS BEEN DONE  IF YOU WANT TO EMPTY IT THERE IS A COMMAND TO DO THAT equation.length = 0;
                      }
    Thank you so much sunfighter! Your help is much appreciated.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    And you have one *HUGE* mistake in there:
    Code:
              else{
                      operation = pressed;
                      num1=equation.join('');
                      var equation = new Array();
                      }
            }
    But using var there, you are OVERRIDING the global var of that same name, and so nothing works.

    ** EDIT ** Oops, a little to slow. BUT... but your code will work if you just remove the var; you don't have to change equation.length (though that works as well).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New Coder
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    And you have one *HUGE* mistake in there:
    Code:
              else{
                      operation = pressed;
                      num1=equation.join('');
                      var equation = new Array();
                      }
            }
    But using var there, you are OVERRIDING the global var of that same name, and so nothing works.

    ** EDIT ** Oops, a little to slow. BUT... but your code will work if you just remove the var; you don't have to change equation.length (though that works as well).
    Oh okay, that makes sense. Thank you guys for your help, now to make it work a bit better =D.


  •  

    Posting Permissions

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