Menu

Create a calculator using JavaScript callback function.

In this post we are going to create a calculator using simple HTML and JavaScript callback function. Using callback functions we have to write less code in compare to writing huge line of code using simple JavaScript functions. Callback and promises are the very useful and fastest way to write the code.

Without any further delay, lets start the coding to create a web based simple calculator using the javaScript callback functions. We will discuss the theory part of callback function and promises other article.

Create a html page with name calculator.html. copy the below code snippet and paste in calculator.html file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<html>
<title>Calculator</title>
    <head>
        <script src="js/calculator.js"></script>
    </head>
<body>
<h3>Calculator</h3>
Number 1: <input id="number1" type="number"/><br /><br />
Number 2: <input id="number2" type="number"/><br /><br />
<input type="button" onclick="calc(addNum)" value="Add" id="add" />
<input type="button" value="Multiply" onclick="calc(multiplyNum)" id="multiply" />
<input type="button" value="Divide" id="divide" onclick="calc(divideNum)" />
<br /><br />
Result: <input id="result" type="number"/><br />
</body>

</html>

Now create a folder with name js in the same directory where you have created your html page, this js folder is to separate the js file from other files.

Now open the js folder and create a calculator.js file.

Copy the below js code snippet the paste it in your calculator.js file.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
function calc(callback) {
    var n1 = parseFloat(document.getElementById("number1").value);
    var n2 = parseFloat(document.getElementById("number2").value);
    var result = callback(n1, n2);
    document.getElementById("result").value = result;
}

function multiplyNum(n1, n2) {
    return n1 * n2;
}
function divideNum(n1, n2) {
    return n1 / n2;
}
function addNum(n1, n2) {
    return n1 + n2;
}

Now open your calculator.html file in browser and see the result. below is the screen shot of the calculator.

callback function calculator-rashidjorvee
callback function calculator

You may download the complete exercise from GitHub https://github.com/RashidJorvee/callbackFunctionJS

ORA-12170: TNS:Connect timeout occurred

When we face "ORA-12170: TNS Connect timeout occurred", then immediately check the following 

  • TNS_ADMIN environment variable is defined or not
  • ORACLE_HOME environment variable is defined as "C:\oraclexe\app\oracle\product\11.2.0\server". 
  • "C:\oraclexe\app\oracle\product\11.2.0\server\Network\Admin\sqlnet.ora" was located NAMES.DIRECTORY_PATH specifies EZCONNECT in the sqlnet.ora file
  • EZCONNECT is not the first naming method in NAMES.DIRECTORY_PATH which may conflict with other naming methods specified

Steps need to follow to fix the issue

  • Try moving EZCONNECT to the first position in NAMES.DIRECTORY_PATH in the sqlnet.ora file
  • Verify that the database server is running
  • The following profile parameters may need to be set to larger values in the sqlnet.ora file on the database server: SQLNET.INBOUND_CONNECT_TIMEOUT, SQLNET.SEND_TIMEOUT, and SQLNET.RECV_TIMEOUT
  • Try to TNSPing (DESCRIPTION=(ADDRESS=(PORT=<port number> e.g. 1521)(HOST=<IP address> e.g. 10.20.30.40)(PROTOCOL=TCP))) using the TNS Ping button on the toolbar

Consider defining a bean of type in your configuration

A compile-time error thrown by Spring application as following "Consider defining a bean of type 'org.sling.service.ServiceClass' in your configuration". This means we have created a class or interface and trying to use it as a service or bean, but your class or interface doesn't have the feature of bean. 
So, to fix this with a service class use annotation @Service [org.springframework.stereotype.Service] on top of your class declaration will fix this error.