Tuesday, December 25, 2012

Add two numbers

Adding Two numbers

This post focuses on adding two numbers, adding two numbers does not mean static value but dynamic inputs with a simple number validation. Below is the step by step procedure.

Create a index.html and place the below code in the body section of the HTML file. 

<!DOCTYPE HTML>
<head>
<title>Add two numbers</title>
<script type="text/javascript" src="script.js" >
</head>
<body>
Enter the first number: <input type="text" id="txt1" /><br />
Enter the seccond number: <input type="text" id="txt2" /><br />
<input type="button" onclick="call()" value="Add"/>
</body>
</html>

Create a javascript file(script.js) with the below content.

function call(){
var q=parseInt(document.getElementById("txt1").value);
var w=parseInt(document.getElementById("txt2").value);
var result=q+w;
   if(isNaN(q)||isNaN(w)){
          alert("please enter a number");
     }
     else
        {
         var result=q+w;
         alert("The sum is " +result);
       }
}

Please do not forget to include the javascript file(script.js) in the index.html.
In this example you would have observed the following javascript terms.
  • onclick
  • document.getElementById
  • parseInt
  • isNaN


1. onclick-In the above example onclick it calls the function call();
2. document.getElementById -gets the values entered in the input fields which defined with id's txt1 and txt2.
3.parseInt - converts the value to Int type.
4.isNaN - checks if the value is NaN.

Couple of things to be noted
  1. document.getElementById always returns a string value, so we need to convert the value to Int(parseInt) for mathematical operations.
  2. In order to ensure that the entered inputs are only numbers , a if condition is implemented to check the values. If the entered value is not a number then it would alert with(please enter a number).
Thorough this example we can add dynamic inputs and validate.

Hope this post was informative, would be adding some more post shortly.



Informative Javascript

34 comments:

  1. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.

    rpa training in bangalore
    best rpa training in bangalore
    RPA training in bangalore
    rpa course in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete
  2. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    AWS Training in pune
    AWS Online Training
    AWS Training in Bangalore

    ReplyDelete
  3. Attend The Python Training in Bangalore From ExcelR. Practical Python Training in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python Training in Bangalore.

    ReplyDelete
  4. Really very informative post you shared here.It's a great skill to make even the person who doesn't know about the subject could able to understand the subject .Keep it up!! Machine Learning Training in Bangalore

    ReplyDelete
  5. ood Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge, keep blogging!! Machine Learning Course

    ReplyDelete
  6. Visit for AWS training in Bangalore:- AWS training in Bangalore

    ReplyDelete
  7. Good Post! , it was so good to read and useful to improve my knowledge as an updated one, keep blogging.After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
    https://www.3ritechnologies.com/course/mern-stack-training-in-pune/

    ReplyDelete
  8. Wow such an amazing content keep it up. I have bookmarked your page to check out more informative content here.

    SASVBA Delhi provides extensive MERN STACK COURSE IN DELHI The extensive hands-on session, hosted by the MERN Stack Training Institute in Delhi, includes live projects and simulations.

    FOR MORE INFO:

    ReplyDelete
  9. Annabelle loves to write and has been doing so for many years.iamlinkfeeder1 iamlinkfeeder1 iamlinkfeeder1

    ReplyDelete
  10. Impressive. Your story always bring hope and new energy. Keep up the good work.
    Best Data Science courses in Hyderabad

    ReplyDelete
  11. This post is so interactive and informative.keep update more information…
    Android Training in Anna Nagar
    Android Training in Chennai

    ReplyDelete
  12. This post is so interactive and informative.keep update more information...
    Data Science course in Tambaram
    Data Science course in Chennai

    ReplyDelete
  13. I am really happy to say it’s an interesting post to read. I learn new information from your article, you are doing a great job. Keep it up
    Software Testing Tools Training in Hyderabad

    ReplyDelete
  14. wordpress web design company in united states Need professional WordPress Web Design Services? We're experts in developing attractive mobile-friendly WordPress websites for businesses. Contact us today!

    ReplyDelete
  15. Unified communications and Ip Pbx includes the connection of various communication systems both for the collaboration tools as the digital workforce.

    ReplyDelete