Search code examples
javascriptjqueryeval

eval() method, with a variable in jQuery syntax, is not working


I am trying to run a calculator with smallest possible JavaScript code bits. I am using jQuery to further minimize my script section. But my eval() method is not working though alert(executed) present just next to it is working fine! What wrong am I doing? Take a look at what I've tried so far!

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  <meta charset="UTF-8" />
  <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.js"></script>
  <meta name="viewport" content="wclassth=device-wclassth, initial-scale=1.0">
  <style>
    .inp {
      font-weight: bolder;
      font-size: 5vw;
      width: 13vw;
      height: 13vw;
      margin-bottom: 2vw;
      margin-top: 2vw;
    }
    
    input:hover {
      color: red;
    }
  </style>
</head>

<body>
  <p>
    Answer will be shown here:
  </p>
  <br></br><br></br>
  <table style="background:black; padding:2vw; width:60vw; margin-left:20vw;">
    <th>
      <div id="display" style="width:56vw; height:15vw; background:skyblue; font-weight:bolder; font-size:5vw; color:red;"></div>
    </th>
    <tr>
      <td>
        <input class="inp" type="button" value="1" />
        <input class="inp" type="button" value="2" />
        <input class="inp" type="button" value="3" />
        <input class="inp" type="button" value="+" />
      </td>
    </tr>
    <tr>
      <td>
        <input class="inp" type="button" value="4" />
        <input class="inp" type="button" value="5" />
        <input class="inp" type="button" value="6" />
        <input class="inp" type="button" value="-" />
      </td>
    </tr>
    <tr>
      <td>
        <input class="inp" type="button" value="7" />
        <input class="inp" type="button" value="8" />
        <input class="inp" type="button" value="9" />
        <input class="inp" type="button" value="*" />
      </td>
    </tr>
    <tr>
      <td>
        <input class="inp" type="button" value="." />
        <input class="inp" type="button" value="0" />
        <input class="inp" type="button" value="/" />
        <input class="inp" type="button" value="ans" />
      </td>
    </tr>
  </table>
  <script>
    $(function() {
      $("input.inp").on("click", function() {
        if ($(this).val() != "ans") {
          var solve = $("#display").append($(this).val());
        }
        if ($(this).val() == "ans") {
          var result = eval(solve);
          $("p").html(eval(result));
          alert("executed");
        }
      });
    });
  </script>
</body>

</html>

Edit: There were some syntax error in this code, which I hope is now fixed.


Solution

  • You need to get value of your #display and then pass the same to your eval function to get required answer.

    Demo Code :

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
      <meta charset="UTF-8" />
      <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.js"></script>
      <meta name="viewport" content="wclassth=device-wclassth, initial-scale=1.0">
      <style>
        .inp {
          font-weight: bolder;
          font-size: 5vw;
          width: 13vw;
          height: 13vw;
          margin-bottom: 2vw;
          margin-top: 2vw;
        }
        
        input:hover {
          color: red;
        }
      </style>
    
      <p>
        Answer will be shown here: <span></span>
      </p>
      <br></br><br></br>
      <table style="background:black; padding:2vw; wclassth:60vw; margin-left:20vw;">
        <th>
          <div id="display" style="wclassth:56vw; height:15vw; background:skyblue; font-weight:bolder; font-size:5vw; color:red;"></div>
        </th>
        <tr>
          <td>
            <input class="inp" type="button" value="1" />
            <input class="inp" type="button" value="2" />
            <input class="inp" type="button" value="3" />
            <input class="inp" type="button" value="+" />
          </td>
        </tr>
        <tr>
          <td>
            <input class="inp" type="button" value="4" />
            <input class="inp" type="button" value="5" />
            <input class="inp" type="button" value="6" />
            <input class="inp" type="button" value="-" />
          </td>
        </tr>
        <tr>
          <td>
            <input class="inp" type="button" value="7" />
            <input class="inp" type="button" value="8" />
            <input class="inp" type="button" value="9" />
            <input class="inp" type="button" value="*" />
          </td>
        </tr>
        <tr>
          <td>
            <input class="inp" type="button" value="." />
            <input class="inp" type="button" value="0" />
            <input class="inp" type="button" value="/" />
            <input class="inp" type="button" value="ans" />
          </td>
        </tr>
      </table>
      <script>
        $(function() {
          $("input.inp").on("click", function() {
    
            if ($(this).val() != "ans") {
         $("#display").append($(this).val());//append new value
             
            }
            if ($(this).val() == "ans") {
           var solve = $("#display").text();//get value from display
              var result = eval(solve);//eval
              console.log(result)
              $("span").html(result);
              alert("executed");
            }
          });
        });
      </script>