How to Use JavaScript to Do Calculation and Make Decisions in Your Website???…

How to Use JavaScript to Do Calculation and Make Decisions in Your Website???…

Now you know that how to store data in JavaScript program, now it’s time to do make some mathematical operations and have fun with it. As usual in your school days you did math operations like addition, subtraction, multiplication, division, in programming they all work same and we call them operators. The combination of at least two operands and an operator make an expression. These expressions have some defined order of evaluation, which tells which expression should have to be evaluated first.

An easy way to remember the order of a evaluation using a mnemonic, PEMDAS (Parenthesis, Express, Multiplication, Division, Addition, Subtraction) which tells parenthesis are first to evaluate then expression within it then multiply, division, addition and then at last subtraction operator is evaluated.

22.3/11/1215.33(12+15)2.56722.3 / 11 / 12 * 15.33 (12 +15) - 2.567

(think… which will be evaluated here first)

Apart from the above, there are some other commonly used operators in programming one of which are increment operator (++) for adding by one and decrement operator (–) for subtracting by one each time when loop runs. For example, you add one to a variable number1 as number1 = number1+1, similarly for decrement you will do number1 = number1 - 1. To shorten up this process, we write number1++ and number1-- to do the same thing.

number2 *= 15; //multiply **number2** variable with 15 
mySalary += 10; //add **mySalary** variable with 10

Data-type Conversion:

There are some confusions in JavaScript when you assume strings as number. For example, when you add strings e.g. “15’” and “12” this will confuse the program when you add another string “abc” not but number. It’s all about order of strings and number here… So: 1 + 2 + ‘abc’ evaluates as **3abc**, whereas: ‘abc’ + 1 + 2 will be evaulated as “abc12”.

In these situations, we have to use data conversion function like parseInt(), parseFloat(), convertToInt(), and these conversion functions will be ignored by default when your data is already converted to desired type. For example:

var myString = "12 Centigrade is my northern areas temperature";

//Conversion from String to Int      
parseInt(myString); 

//Assigned to another variable        
var myInt = parseInt(myString);  

Your string shouldn’t start with a number or it will confuse the conversion process and you’ll get NaN (Not a Number). For these cases, use IsNaN() function to validate whether it consists a number.

Comparison Statements:

1- IF STATEMENT

2- IF ELSE STATEMENT

3- NESTED IF ELSE STATEMENT

COMPARISON OPERATORS: These operator help to comparison about true or false for example, whether one event happen or not. Two events found equal like today it rained 10% more than last day. We’ll use comparison operators. Simply, we can say use these to check if one operand is greater/less than or equal than other)

Sign
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
!= Not equal to

1. Logical Statements:

LOGICAL (to check if logical statement is true or false)

Sign
&& AND binary operator
|| OR binary operator
& AND bit operator
| OR bit operator
! NOT operator

Let’s have some practice on using combination off comparison and logical operators. here we are using and if else statement to check whether a person’s age is between zero or 10. We will do some more interesting use of these operators in later articles…

if (myAge >= 0 && myAge <= 10){ 
     document.write(“You’re a child”);
}  else if ( !(myAge >= 0 && myAge <= 10) ){ 
     document.write(“You’re not a child”);
}  

2. Switch Statement

Switch statements are an alternative to if-else statements. They are faster to evaluate the if-else statement and are best to use when you have to create menu or need to make some quick selections. This is how you can write a switch statement in JavaScript:

switch (userChoice){ 
	case 1:   
          document.write(“Open Workbook”);
          break;
     case 2:   
          document.write(“View Today’s Sales”);
          break;
     case 3:   
          document.write(“Delete Records”);   
          break;
     case 4:   
          document.write(“View Month’s Sales”);   
          break;
     case 5:   
          document.write(“Quit”);
          break;
          
     default:   
          document.write(“You did not enter a number between 1 and 5.);
          break; 
}

3. Looping / Iterations:

When ever you want to repeat a specific block of court for several number of time and unspecified number of time until a condition met, you use loops. There are several types of loops in JavaScript, for loop, while loop, do while loop and a new one for each loop. If you’re a Java, C# or a C programmer then don’t worry they all written and work the same way:

//Best for limited number of iterations
for(int i=0; i< n; i++) {
     //Your statements
}

//Best for unlimited number of iterations until a specific condition met 
while(condition) {
     //Your statements 
}

//Run at least once and work same as while loop do
{
     //Your statements 
}while(condition)

For each or for…in loop is a new addition of loop. This loop works primarily with arrays and also with something called objects. It works when elements in array is not certain and you want to loop through all indexes of array and the good news it does it automatically until you find you desired element. As the name implied “For each element in the array”. It is also written the same way:

var index;
var myArray = new Array(“James”, “Tiffany”, “Jameel”, “Jack”) 

for(index in myArray) {    
	document.write(myArray[index]);
}

Conclusion:

Now, it’s your turn to pick any of mathematical and decision making problem near you and create a JavaScript
e.g. finding a highest number, gradebook, balance sheet, or a lottery program. Oh I just forgot to tell you for this you’ll need to know how to make random numbers, we’ll discuss this soon. At this moment, this is all for you to learn and get ready for upcoming posts.

Comments

Popular posts from this blog

How HTML Forms Works with JavaScript and Optimize Your Productivity with JQuery

My Mission