kiler1233
Ad Revenue Expert
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
In this tutorial we will create a Get Average Number Automatically using JavaScript. This code will auto calculate the average of a number when user enter a number in the text input. The code itself use JavaScript onkeyup() to automatically call a specific function that calculate the given numbers by getting the sum of all numbers divided by the number of the text inputs. This code is free to use, you can modify it as your own,
We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .
Getting started:
First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.
The Main Interface
This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
Creating the Script
This code contains the script of the application. This code will calculate a given number automatically when entered. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
There you have it we successfully created a Get Average Number Automatically using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Download
We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .
Getting started:
First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.
The Main Interface
This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
- <!DOCTYPE html>
- <html
lang
=
"en"
>
- <head
>
- <meta
charset
=
"UTF-8"
name
=
"viewport"
content
=
"width=device-width, initial-scale=1"
/
>
- <link
rel
=
"stylesheet"
type
=
"text/css"
href
=
"css/bootstrap.css"
/
>
- </
head
>
- <body
>
- <nav class
=
"navabar navbar-default"
>
- <div
class
=
"container-fluid"
>
- <a
class
=
"navbar-brand"
href
=
"https://sourcecodester.com"
>
Sourcecodester</
a
>
- </
div
>
- </
nav>
- <div
class
=
"col-md-3"
></
div
>
- <div
class
=
"col-md-6 well"
>
- <h3
class
=
"text-primary"
>
JavaScript - Get Average Number Automatically</
h3
>
- <hr
style
=
"border-top:1px dotted;"
/
>
- <div
class
=
"col-md-4"
>
- <div
class
=
"form-group"
>
- <label
>
Grade #1</
label
>
- <input
type
=
"number"
id
=
"grade1"
onkeyup
=
"calculateAve();"
class
=
"form-control"
/
>
- </
div
>
- <div
class
=
"form-group"
>
- <label
>
Grade #2</
label
>
- <input
type
=
"number"
id
=
"grade2"
onkeyup
=
"calculateAve();"
class
=
"form-control"
/
>
- </
div
>
- <div
class
=
"form-group"
>
- <label
>
Grade #3</
label
>
- <input
type
=
"number"
id
=
"grade3"
onkeyup
=
"calculateAve();"
class
=
"form-control"
/
>
- </
div
>
- </
div
>
- <div
class
=
"col-md-4"
>
- <div
class
=
"form-inline"
>
- <label
>
Average Number</
label
>
- <input
type
=
"text"
class
=
"form-control"
id
=
"result"
readonly
=
"readonly"
/
>
- </
div
>
- </
div
>
- </
div
>
- <script
src
=
"js/script.js"
></
script
>
- </
body
>
- </
html
>
Creating the Script
This code contains the script of the application. This code will calculate a given number automatically when entered. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
- function
calculateAve(
)
{
- var
grade1=
document.getElementById
(
'grade1'
)
.value
;
- var
grade2=
document.getElementById
(
'grade2'
)
.value
;
- var
grade3=
document.getElementById
(
'grade3'
)
.value
;
- var
total;
- var
ave;
- if
(
grade1 !=
""
&&
grade2 !=
""
&&
grade3 !=
""
)
{
- total=
parseInt(
grade1)
+
parseInt(
grade2)
+
parseInt(
grade3)
;
- ave=
total/
3
- document.getElementById
(
'result'
)
.value
=
ave.toFixed
(
0
)
;
- }
- }
There you have it we successfully created a Get Average Number Automatically using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Download
You must upgrade your account or reply in the thread to view the hidden content.