A little bit of JS (JavaScript)
Hi Everybody today i'm going to show you how to create a simple calculator, with this we are going to learn:
Ok once we have unterstood how JS works now lets create our HTML file:
<html>
function erase(form){
</form>
Ok now for explain to you the code I'm using color codes:
As you have seen, it is a simple Calculator with the 4 main functions, but in the previous code I did not put the code for the functions, the code for the functions is:
function sum(v1,v2){
function res(v1,v2){
function mul(v1,v2){
function div(v1,v2){
But how this Calculator works? The Calculator works like the following picture:
As you can see I have splited the Js Code in 2 because with one part I control the actions of the buttons and with the other part, well the other part just perform the operations, And How did I do this? Simple, let's see the Js Code of the HTML File:
<script>
function erase(form){
<script src="functions.js"></script>
As we can see the blue part is the controler of the HTML document, I mean, is the code which control the buttons and according to the events display the expected result, and the red part is a separated document which contains all the functions to be performed.
The calculator will work like this:
As we can see this documents follow MVC pattern because we have our view (Calculator Screen), our controller(Js Controller) and the Model (Js Document). It is easy to understand, the View will display the view of the page, will display what the user is going to see. When an user perform an action, the controller will evaluate this action and according to this action will invoke the corresponding function and finally, the Model will return the expected result to the view for the previous user's action.
Ok and now for the closure, we have learned this:
<script>
<script>
sum=parseFloat(v1)+parseFloat(v2)
In the future I will show the different parsing existing in Js
So chavoz(here in Mexico 'chavos' or 'chavos' is like dudes) this is it for now, I hope I can help you with this at least a little bit, see you in the next time ^_^/ and if you have doubts or comments, please leave them in that section below.
- How to combine Js with HTML
- How to manage the attributes of a Form through Js
- Conditional Sentences with Js
- Global Variables with Js
- Parsing values in Js
- Notepad
- Internet browser
Ok once we have unterstood how JS works now lets create our HTML file:
<html>
<head>
<title>My First Calculator with JS</title>
<script>
var val1=0
var sig=0
function show(form){
if(val1==0){
val1=document.myform.screen.value
document.myform.screen.value=0
}
switch(form){
case "+":
val1=sum(val1,document.myform.screen.value)
document.myform.screen.value=0
sig=1
document.myform.point.disabled=false
break
case "-":
val1=res(val1,document.myform.screen.value)
document.myform.screen.value=0
sig=2
document.myform.point.disabled=false
break
case "*":
document.myform.screen.value=1
val1=mul(val1,document.myform.screen.value)
document.myform.screen.value=0
sig=3
document.myform.point.disabled=false
break
case "/":
document.myform.screen.value=1
val1=div(val1,document.myform.screen.value)
document.myform.screen.value=0
sig=4
document.myform.point.disabled=false
break
case "=":
if(document.myform.screen.value!=0){
switch(sig){
case 1:
val1=sum(val1,document.myform.screen.value)
break
case 2:
val1=res(val1,document.myform.screen.value)
break
case 3:
val1=mul(val1,document.myform.screen.value)
break
case 4:
val1=div(val1,document.myform.screen.value)
break
}
}
document.myform.screen.value=val1
val1=0
sig=0
document.myform.point.disabled=false
break
}
}
form.screen.value="0"
form.point.disabled=false
val1=0
sig=0
}
function numbers(val){
var con=document.myform.screen.value+""+val
if (val=="."){
document.myform.screen.value=con
document.myform.point.disabled=true
}
else{
var num=parseFloat(con)
document.myform.screen.value=num
}
}
</script>
<script src="functions.js"></script>
</head>
<body>
<table>
<form name="myform">
<tr>
<td colspan=3><input type="text" size=10 name="screen" value="0" disabled /></td>
<td><input type="button" id="C" value="C" onClick="erase(this.form)" /></td>
</tr>
<tr align="center">
<td><input type="button" id="one" value="1" onClick="numbers(this.value)"></td>
<td><input type="button" id="two" value="2" onClick="numbers(this.value)"></td>
<td><input type="button" id="three" value="3" onClick="numbers(this.value)"></td>
<td><input type="button" id="plus" value="+" onClick="show(this.value)"></td>
</tr>
<tr align="center">
<td><input type="button" id="four" value="4" onClick="numbers(this.value)"></td>
<td><input type="button" id="five" value="5" onClick="numbers(this.value)"></td>
<td><input type="button" id="six" value="6" onClick="numbers(this.value)"></td>
<td><input type="button" id="min" value="-" onClick="show(this.value)"></td>
</tr>
<tr align="center">
<td><input type="button" id="seven" value="7" onClick="numbers(this.value)"></td>
<td><input type="button" id="eight" value="8" onClick="numbers(this.value)"></td>
<td><input type="button" id="nine" value="9" onClick="numbers(this.value)"></td>
<td><input type="button" id="mul" value="*" onClick="show(this.value)"></td>
</tr>
<tr align="center">
<td><input type="button" id="zero" value="0" onClick="numbers(this.value)"></td>
<td><input type="button" id="point" value="." onClick="numbers(this.value)"></td>
<td><input type="button" id="equ" value="=" onClick="show(this.value)"></td>
<td><input type="button" id="div" value="/" onClick="show(this.value)"></td>
</tr>
</table>
</body>
</html>
Ok now for explain to you the code I'm using color codes:
- The blue one belongs to all the code of HTML
- The red one belongs to all the code of JavaScript
As you have seen, it is a simple Calculator with the 4 main functions, but in the previous code I did not put the code for the functions, the code for the functions is:
function sum(v1,v2){
var sum
sum=parseFloat(v1)+parseFloat(v2)
return sum
}
var res
res=v1-v2
return res
}
var mul
mul=v1*v2
return mul
}
var div
div=v1/v2
return div
}
As you can see I have splited the Js Code in 2 because with one part I control the actions of the buttons and with the other part, well the other part just perform the operations, And How did I do this? Simple, let's see the Js Code of the HTML File:
<script>
var val1=0
var sig=0
function show(form){
if(val1==0){
val1=document.myform.screen.value
document.myform.screen.value=0
}
switch(form){
case "+":
val1=sum(val1,document.myform.screen.value)
document.myform.screen.value=0
sig=1
document.myform.point.disabled=false
break
case "-":
val1=res(val1,document.myform.screen.value)
document.myform.screen.value=0
sig=2
document.myform.point.disabled=false
break
case "*":
document.myform.screen.value=1
val1=mul(val1,document.myform.screen.value)
document.myform.screen.value=0
sig=3
document.myform.point.disabled=false
break
case "/":
document.myform.screen.value=1
val1=div(val1,document.myform.screen.value)
document.myform.screen.value=0
sig=4
document.myform.point.disabled=false
break
case "=":
if(document.myform.screen.value!=0){
switch(sig){
case 1:
val1=sum(val1,document.myform.screen.value)
break
case 2:
val1=res(val1,document.myform.screen.value)
break
case 3:
val1=mul(val1,document.myform.screen.value)
break
case 4:
val1=div(val1,document.myform.screen.value)
break
}
}
document.myform.screen.value=val1
val1=0
sig=0
document.myform.point.disabled=false
break
}
}
form.screen.value="0"
form.point.disabled=false
val1=0
sig=0
}
function numbers(val){
var con=document.myform.screen.value+""+val
if (val=="."){
document.myform.screen.value=con
document.myform.point.disabled=true
}
else{
var num=parseFloat(con)
document.myform.screen.value=num
}
}
</script>
As we can see the blue part is the controler of the HTML document, I mean, is the code which control the buttons and according to the events display the expected result, and the red part is a separated document which contains all the functions to be performed.
The calculator will work like this:
Calculator Screen | < - > | Js Controller | < - > | Js Document(Functions) |
As we can see this documents follow MVC pattern because we have our view (Calculator Screen), our controller(Js Controller) and the Model (Js Document). It is easy to understand, the View will display the view of the page, will display what the user is going to see. When an user perform an action, the controller will evaluate this action and according to this action will invoke the corresponding function and finally, the Model will return the expected result to the view for the previous user's action.
Ok and now for the closure, we have learned this:
- How to combine Js with HTML
<script>
var val1=0
var sig=0
function show(form){
if(val1==0){
val1=document.myform.screen.value
document.myform.screen.value=0
}
...
</script>
- How to manage the attributes of a Form through Js
- Conditional Sentences with Js
if(val1==0){
val1=document.myform.screen.value
document.myform.screen.value=0
}
The use and the sintax of the conditional Sentences are the same as in other languages as Java or C#, in this post I have used the if - else and the switch clause
- Global Variables with Js
<script>
var val1=0
var sig=0
...
</script>
- Parsing values in Js
sum=parseFloat(v1)+parseFloat(v2)
In the future I will show the different parsing existing in Js
So chavoz(here in Mexico 'chavos' or 'chavos' is like dudes) this is it for now, I hope I can help you with this at least a little bit, see you in the next time ^_^/ and if you have doubts or comments, please leave them in that section below.
Comments
Post a Comment