Skip to main content
Tweeted twitter.com/StackCodeReview/status/863033944660430848
added 92 characters in body; edited tags; edited title
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Code analysis for js JavaScript calculator with two inputs and four operations

    <html>
  <script>
    
    var inputIdFirst = "valueOfX",inputIdSecond = "valueOfY",outputId = "resultHere";
    var getInputs = function(id) {
      return parseInt(document.getElementById(id).value);
    }
    var showOutput = function(outputValue, outputIdAsArg) {
      //if no argument is given then by default "outputId" taken
      document.getElementById(outputId).innerHTML = outputValue;
    }
    var manuplateAs = function(operationName, valueOfX, valueOfY) {
      if(operationName == 'add')
        return valueOfX + valueOfY;
      else if(operationName == 'sub')
        return valueOfX - valueOfY;
      else if(operationName == 'mul')
        return valueOfX * valueOfY;
      else if(operationName == 'div')
        return valueOfX / valueOfY;
      //can add as many as you wish 
    } 
    var operation = function(operationName){
      x = getInputs(inputIdFirst);
      y = getInputs(inputIdSecond);
      output = manuplateAs(operationName, x, y);
      showOutput(output);
      console.log(x + " " + operationName + " " + y + " = " + output)
      //see console to understand this more
      console.log(this)
    };
  </script>
</head>
<body>
      Enter first number
      <input type="number" id="valueOfX">
      Enter second number
      <input type="number" id="valueOfY">
      <button onclick="operation('add')"> Add </button>
      <button onclick="operation('sub')"> Sub</button>
      <button onclick="operation('mul')"> Mult</button>
      <button onclick="operation('div')"> Divi</button>
      <h1 id="resultHere"></h1>
</body>
</html>

    <html>
  <script>
    
    var inputIdFirst = "valueOfX",inputIdSecond = "valueOfY",outputId = "resultHere";
    var getInputs = function(id) {
      return parseInt(document.getElementById(id).value);
    }
    var showOutput = function(outputValue, outputIdAsArg) {
      //if no argument is given then by default "outputId" taken
      document.getElementById(outputId).innerHTML = outputValue;
    }
    var manuplateAs = function(operationName, valueOfX, valueOfY) {
      if(operationName == 'add')
        return valueOfX + valueOfY;
      else if(operationName == 'sub')
        return valueOfX - valueOfY;
      else if(operationName == 'mul')
        return valueOfX * valueOfY;
      else if(operationName == 'div')
        return valueOfX / valueOfY;
      //can add as many as you wish 
    } 
    var operation = function(operationName){
      x = getInputs(inputIdFirst);
      y = getInputs(inputIdSecond);
      output = manuplateAs(operationName, x, y);
      showOutput(output);
      console.log(x + " " + operationName + " " + y + " = " + output)
      //see console to understand this more
      console.log(this)
    };
  </script>
</head>
<body>
      Enter first number
      <input type="number" id="valueOfX">
      Enter second number
      <input type="number" id="valueOfY">
      <button onclick="operation('add')"> Add </button>
      <button onclick="operation('sub')"> Sub</button>
      <button onclick="operation('mul')"> Mult</button>
      <button onclick="operation('div')"> Divi</button>
      <h1 id="resultHere"></h1>
</body>
</html>

Code analysis for js calculator

    <html>
  <script>
    
    var inputIdFirst = "valueOfX",inputIdSecond = "valueOfY",outputId = "resultHere";
    var getInputs = function(id) {
      return parseInt(document.getElementById(id).value);
    }
    var showOutput = function(outputValue, outputIdAsArg) {
      //if no argument is given then by default "outputId" taken
      document.getElementById(outputId).innerHTML = outputValue;
    }
    var manuplateAs = function(operationName, valueOfX, valueOfY) {
      if(operationName == 'add')
        return valueOfX + valueOfY;
      else if(operationName == 'sub')
        return valueOfX - valueOfY;
      else if(operationName == 'mul')
        return valueOfX * valueOfY;
      else if(operationName == 'div')
        return valueOfX / valueOfY;
      //can add as many as you wish 
    } 
    var operation = function(operationName){
      x = getInputs(inputIdFirst);
      y = getInputs(inputIdSecond);
      output = manuplateAs(operationName, x, y);
      showOutput(output);
      console.log(x + " " + operationName + " " + y + " = " + output)
      //see console to understand this more
      console.log(this)
    };
  </script>
</head>
<body>
      Enter first number
      <input type="number" id="valueOfX">
      Enter second number
      <input type="number" id="valueOfY">
      <button onclick="operation('add')"> Add </button>
      <button onclick="operation('sub')"> Sub</button>
      <button onclick="operation('mul')"> Mult</button>
      <button onclick="operation('div')"> Divi</button>
      <h1 id="resultHere"></h1>
</body>
</html>

JavaScript calculator with two inputs and four operations

    <html>
  <script>
    
    var inputIdFirst = "valueOfX",inputIdSecond = "valueOfY",outputId = "resultHere";
    var getInputs = function(id) {
      return parseInt(document.getElementById(id).value);
    }
    var showOutput = function(outputValue, outputIdAsArg) {
      //if no argument is given then by default "outputId" taken
      document.getElementById(outputId).innerHTML = outputValue;
    }
    var manuplateAs = function(operationName, valueOfX, valueOfY) {
      if(operationName == 'add')
        return valueOfX + valueOfY;
      else if(operationName == 'sub')
        return valueOfX - valueOfY;
      else if(operationName == 'mul')
        return valueOfX * valueOfY;
      else if(operationName == 'div')
        return valueOfX / valueOfY;
      //can add as many as you wish 
    } 
    var operation = function(operationName){
      x = getInputs(inputIdFirst);
      y = getInputs(inputIdSecond);
      output = manuplateAs(operationName, x, y);
      showOutput(output);
      console.log(x + " " + operationName + " " + y + " = " + output)
      //see console to understand this more
      console.log(this)
    };
  </script>
</head>
<body>
      Enter first number
      <input type="number" id="valueOfX">
      Enter second number
      <input type="number" id="valueOfY">
      <button onclick="operation('add')"> Add </button>
      <button onclick="operation('sub')"> Sub</button>
      <button onclick="operation('mul')"> Mult</button>
      <button onclick="operation('div')"> Divi</button>
      <h1 id="resultHere"></h1>
</body>
</html>

Source Link
Praveen Soni
  • 235
  • 1
  • 3
  • 8

Code analysis for js calculator

i build a calc & want to a review that what i did wrong & what is right method to do.

    <html>
  <script>
    
    var inputIdFirst = "valueOfX",inputIdSecond = "valueOfY",outputId = "resultHere";
    var getInputs = function(id) {
      return parseInt(document.getElementById(id).value);
    }
    var showOutput = function(outputValue, outputIdAsArg) {
      //if no argument is given then by default "outputId" taken
      document.getElementById(outputId).innerHTML = outputValue;
    }
    var manuplateAs = function(operationName, valueOfX, valueOfY) {
      if(operationName == 'add')
        return valueOfX + valueOfY;
      else if(operationName == 'sub')
        return valueOfX - valueOfY;
      else if(operationName == 'mul')
        return valueOfX * valueOfY;
      else if(operationName == 'div')
        return valueOfX / valueOfY;
      //can add as many as you wish 
    } 
    var operation = function(operationName){
      x = getInputs(inputIdFirst);
      y = getInputs(inputIdSecond);
      output = manuplateAs(operationName, x, y);
      showOutput(output);
      console.log(x + " " + operationName + " " + y + " = " + output)
      //see console to understand this more
      console.log(this)
    };
  </script>
</head>
<body>
      Enter first number
      <input type="number" id="valueOfX">
      Enter second number
      <input type="number" id="valueOfY">
      <button onclick="operation('add')"> Add </button>
      <button onclick="operation('sub')"> Sub</button>
      <button onclick="operation('mul')"> Mult</button>
      <button onclick="operation('div')"> Divi</button>
      <h1 id="resultHere"></h1>
</body>
</html>