SlideShare a Scribd company logo
JavaScript Variables, Function and Method
  Parse a value (parseFloat and parseInt)
       Round method(Math.round)
Checking the Assignment
• Using JavaScript write the source code using
   prompt box , alert box and document.write.
1. Converts the user-specified temperature reading
    in
a. Fahrenheit to Celsius
F= ?
C= (F-32)* 5/9
b. Celsius to Kelvin
C= ?
K= C + 273.15
<html>
<body>
<script>
var F= 25
var C= (F-32)*5/9
alert(""+C+"")
document.write(""+C+"");
document.write("",parseInt(C));
</script>
<script>
var fahrenheit= parseFloat(prompt("Enter Fahrenheit "," "));
var celsius= (fahrenheit-32)*5/9
alert(""+celsius+"");
document.write(""+celsius+"");
document.write("",parseInt(celsius));
</script>
•   <script>
•   var C= 25
•   var K= C + 273.15
•   alert(""+K+"");
•   document.write(""+K+"");
•   </script>
•   <script>
• var celsius= parseFloat(prompt("Enter Celsius "," "));
• var kelvin= celsius + 273.15
• alert(""+kelvin+"");
• document.write(""+kelvin+"");
•   <html>
•   <body>
•   <script>
•   var name= prompt("Enter your name "," ");
•   var school= prompt("Enter your school "," ");
•   var yearlevel= prompt("Enter your year level "," ");
•   var section= prompt("Enter your section "," ");
•   document.write(""+name+"" + "<br>");
•   document.write(""+school+"" + "<br>");
•   document.write(""+yearlevel+"" + "<br>");
•   document.write(""+section+"" + "<br>");
•   </script>
•   </body>
•   </html>
JavaScript Variables
• As with algebra, JavaScript variables can be used
  to hold values (x=5) or expressions (z=x+y).
• Variable can have short names (like x and y) or
  more descriptive names (age, sum, totalvolume).
• Variable names must begin with a letter
• Variable names can also begin with $ and _ (but
  we will not use it)
• Variable names are case sensitive (y and Y are
  different variables)
• Both JavaScript statements and JavaScript
  variables are case-sensitive.
JavaScript Data Types
• JavaScript variables can also hold other types of data, like
  text values (name="John Doe").
• In JavaScript a text like "John Doe" is called a string.
• There are many types of JavaScript variables, but for now,
  just think of numbers and strings.
• When you assign a text value to a variable, put double or
  single quotes around the value.
• When you assign a numeric value to a variable, do not put
  quotes around the value. If you put quotes around a
  numeric value, it will be treated as text.
• Example
  var pi=3.14;
  var name="John Doe";
  var answer='Yes I am!';
One Statement, Many Variables
• You can declare many variables in one
  statement. Just start the statement
  with var and separate the variables by
  comma:
• var name="Doe", age=30, job="carpenter";
• Your declaration can also span multiple lines:
• var name="Doe",
  age=30,
  job="carpenter";
JavaScript parseInt() Function

• Definition and Usage
--------The parseInt() function parses a string and
   returns an integer.

• Note: Only the first number in the string is returned!
• Note: Leading and trailing spaces are allowed.
• Note: If the first character cannot be converted to a number,
  parseInt() returns NaN.
EXAMPLE                                           OUTPUT
<script>

document.write(parseInt("10") + "<br>");          10
document.write(parseInt("10.33") + "<br>");       10
document.write(parseInt("34 45 66") + "<br>");    34
document.write(parseInt(" 60 ") + "<br>");        60
document.write(parseInt("40 years") + "<br>");    40
document.write(parseInt("He was 40") + "<br>");   NaN

</script>
JavaScript round() Method
Definition and Usage
• The round() method rounds a number to the
  nearest integer.
Syntax                       Method
var x= Math.round(10.53)

var y= Math.round(8/5)                Value to be
                                      rounded off
var z= Math.round(mass/volume)
More Examples
Example                      OUTPUT
var a=Math.round(2.60);      3
var b=Math.round(2.50);      3
var c=Math.round(2.49);      2
var d=Math.round(-2.60);     -3
var e=Math.round(-2.50);     -2
var f=Math.round(-2.49);     -2
Write a JavaScript source code for this problem.
Use either alert, prompt, and document. write.


1. In Math, Jesus got 96 for the first grading, and
   95 for the 2nd grading, 90 for the 3rd, and 97 for
   the the 4th grading. What will be his final grade
   in Math.

Note: to find the final grade you have to
1. Add first all the grade from first – fourth grading
2. To get the final grade = sum or total of all the
   grades divided by 4

More Related Content

What's hot (19)

PPT
Modules 17 12_2020
Sugnan M
 
PDF
C++ ARRAY WITH EXAMPLES
Farhan Ab Rahman
 
PDF
Regexp
Ynon Perek
 
PDF
Scala. Introduction to FP. Monads
Kirill Kozlov
 
PPT
C programming , array 2020
Osama Ghandour Geris
 
PPTX
Formal methods 3 - languages and machines
Vlad Patryshev
 
PDF
Python : Regular expressions
Emertxe Information Technologies Pvt Ltd
 
PDF
Character Array and String
Tasnima Hamid
 
PDF
Head First Java Chapter 3
Tom Henricksen
 
PDF
Ch03
Hankyo
 
PPTX
String in c programming
Devan Thakur
 
PPTX
Formal methods 5 - Pi calculus
Vlad Patryshev
 
PDF
2Bytesprog2 course_2014_c6_single linked list
kinan keshkeh
 
PDF
5 2. string processing
웅식 전
 
PPTX
Regular expressions
Brij Kishore
 
Modules 17 12_2020
Sugnan M
 
C++ ARRAY WITH EXAMPLES
Farhan Ab Rahman
 
Regexp
Ynon Perek
 
Scala. Introduction to FP. Monads
Kirill Kozlov
 
C programming , array 2020
Osama Ghandour Geris
 
Formal methods 3 - languages and machines
Vlad Patryshev
 
Python : Regular expressions
Emertxe Information Technologies Pvt Ltd
 
Character Array and String
Tasnima Hamid
 
Head First Java Chapter 3
Tom Henricksen
 
Ch03
Hankyo
 
String in c programming
Devan Thakur
 
Formal methods 5 - Pi calculus
Vlad Patryshev
 
2Bytesprog2 course_2014_c6_single linked list
kinan keshkeh
 
5 2. string processing
웅식 전
 
Regular expressions
Brij Kishore
 

Viewers also liked (7)

PPTX
Html web designing enhancing text
Jesus Obenita Jr.
 
PPTX
Css margins
Webtech Learning
 
PDF
Css In Iterations
Vladimir Zhidal
 
PPTX
Css margin and padding property
Jesus Obenita Jr.
 
PPTX
Java script introduction
Jesus Obenita Jr.
 
PDF
Lab#7 CSS Box Model
Yaowaluck Promdee
 
PPTX
CSS_Day_Three (W3schools)
Rafi Haidari
 
Html web designing enhancing text
Jesus Obenita Jr.
 
Css margins
Webtech Learning
 
Css In Iterations
Vladimir Zhidal
 
Css margin and padding property
Jesus Obenita Jr.
 
Java script introduction
Jesus Obenita Jr.
 
Lab#7 CSS Box Model
Yaowaluck Promdee
 
CSS_Day_Three (W3schools)
Rafi Haidari
 
Ad

Similar to Variables 2 (20)

PPTX
Variables
Jesus Obenita Jr.
 
PPTX
Java script basics
Shrivardhan Limbkar
 
PPTX
JavaScript.pptx
KennyPratheepKumar
 
PPTX
FYBSC IT Web Programming Unit III Core Javascript
Arti Parab Academics
 
PDF
Handout - Introduction to Programming
Cindy Royal
 
PPTX
Introduction to JavaScript - Web Programming
tyfeng7
 
PPTX
Java script
Shagufta shaheen
 
PDF
Javascript - Tutorial
adelaticleanu
 
PPTX
1-JAVA SCRIPT. servere-side applications vs client side applications
surajshreyans
 
PPT
Java Script Introduction
jason hu 金良胡
 
PPSX
Javascript variables and datatypes
Varun C M
 
PPTX
Introduction to Client-Side Javascript
Julie Iskander
 
PPTX
Javascript 101
Shlomi Komemi
 
PPT
An introduction to javascript
MD Sayem Ahmed
 
PPT
Javascript
Manav Prasad
 
PPTX
Learn java script
Mahmoud Asadi
 
PDF
Fewd week5 slides
William Myers
 
PDF
GeoGebra JavaScript CheatSheet
Jose Perez
 
PPTX
Js tutorial(Basic concepts, running a program ,console,variable,types etc..)
reshmy12
 
PPTX
An Introduction to JavaScript
tonyh1
 
Java script basics
Shrivardhan Limbkar
 
JavaScript.pptx
KennyPratheepKumar
 
FYBSC IT Web Programming Unit III Core Javascript
Arti Parab Academics
 
Handout - Introduction to Programming
Cindy Royal
 
Introduction to JavaScript - Web Programming
tyfeng7
 
Java script
Shagufta shaheen
 
Javascript - Tutorial
adelaticleanu
 
1-JAVA SCRIPT. servere-side applications vs client side applications
surajshreyans
 
Java Script Introduction
jason hu 金良胡
 
Javascript variables and datatypes
Varun C M
 
Introduction to Client-Side Javascript
Julie Iskander
 
Javascript 101
Shlomi Komemi
 
An introduction to javascript
MD Sayem Ahmed
 
Javascript
Manav Prasad
 
Learn java script
Mahmoud Asadi
 
Fewd week5 slides
William Myers
 
GeoGebra JavaScript CheatSheet
Jose Perez
 
Js tutorial(Basic concepts, running a program ,console,variable,types etc..)
reshmy12
 
An Introduction to JavaScript
tonyh1
 
Ad

More from Jesus Obenita Jr. (20)

PPTX
Organization and management 3 a Evolution of Management Theory
Jesus Obenita Jr.
 
PPTX
Organization and management 2 Management Function
Jesus Obenita Jr.
 
PPTX
Organization and management 1
Jesus Obenita Jr.
 
PPTX
Designing web page marquee and img tag
Jesus Obenita Jr.
 
PPTX
Ms excel 2013 formatting worksheets
Jesus Obenita Jr.
 
PPTX
Ms excel 2013 data management
Jesus Obenita Jr.
 
PPTX
Microsoft Excel introduction
Jesus Obenita Jr.
 
PPTX
Word 2013 working with pictures
Jesus Obenita Jr.
 
PPTX
Word 2013 Formatting Page
Jesus Obenita Jr.
 
PPTX
Word 2013 8
Jesus Obenita Jr.
 
PPTX
Ms word 2013 7
Jesus Obenita Jr.
 
PPTX
Ms word 2013 6
Jesus Obenita Jr.
 
PPTX
Ms word 2013 4
Jesus Obenita Jr.
 
PPTX
Ms word 2013 2
Jesus Obenita Jr.
 
PPTX
Ms word 2013
Jesus Obenita Jr.
 
PPTX
Parts of the ms word 2013 screen and
Jesus Obenita Jr.
 
PPTX
Word processor
Jesus Obenita Jr.
 
PPT
Session 2 test construction.mt's
Jesus Obenita Jr.
 
PPTX
Cooking ingredients
Jesus Obenita Jr.
 
PPTX
Color theory
Jesus Obenita Jr.
 
Organization and management 3 a Evolution of Management Theory
Jesus Obenita Jr.
 
Organization and management 2 Management Function
Jesus Obenita Jr.
 
Organization and management 1
Jesus Obenita Jr.
 
Designing web page marquee and img tag
Jesus Obenita Jr.
 
Ms excel 2013 formatting worksheets
Jesus Obenita Jr.
 
Ms excel 2013 data management
Jesus Obenita Jr.
 
Microsoft Excel introduction
Jesus Obenita Jr.
 
Word 2013 working with pictures
Jesus Obenita Jr.
 
Word 2013 Formatting Page
Jesus Obenita Jr.
 
Word 2013 8
Jesus Obenita Jr.
 
Ms word 2013 7
Jesus Obenita Jr.
 
Ms word 2013 6
Jesus Obenita Jr.
 
Ms word 2013 4
Jesus Obenita Jr.
 
Ms word 2013 2
Jesus Obenita Jr.
 
Ms word 2013
Jesus Obenita Jr.
 
Parts of the ms word 2013 screen and
Jesus Obenita Jr.
 
Word processor
Jesus Obenita Jr.
 
Session 2 test construction.mt's
Jesus Obenita Jr.
 
Cooking ingredients
Jesus Obenita Jr.
 
Color theory
Jesus Obenita Jr.
 

Variables 2

  • 1. JavaScript Variables, Function and Method Parse a value (parseFloat and parseInt) Round method(Math.round)
  • 2. Checking the Assignment • Using JavaScript write the source code using prompt box , alert box and document.write. 1. Converts the user-specified temperature reading in a. Fahrenheit to Celsius F= ? C= (F-32)* 5/9 b. Celsius to Kelvin C= ? K= C + 273.15
  • 3. <html> <body> <script> var F= 25 var C= (F-32)*5/9 alert(""+C+"") document.write(""+C+""); document.write("",parseInt(C)); </script> <script> var fahrenheit= parseFloat(prompt("Enter Fahrenheit "," ")); var celsius= (fahrenheit-32)*5/9 alert(""+celsius+""); document.write(""+celsius+""); document.write("",parseInt(celsius)); </script>
  • 4. <script> • var C= 25 • var K= C + 273.15 • alert(""+K+""); • document.write(""+K+""); • </script> • <script> • var celsius= parseFloat(prompt("Enter Celsius "," ")); • var kelvin= celsius + 273.15 • alert(""+kelvin+""); • document.write(""+kelvin+"");
  • 5. <html> • <body> • <script> • var name= prompt("Enter your name "," "); • var school= prompt("Enter your school "," "); • var yearlevel= prompt("Enter your year level "," "); • var section= prompt("Enter your section "," "); • document.write(""+name+"" + "<br>"); • document.write(""+school+"" + "<br>"); • document.write(""+yearlevel+"" + "<br>"); • document.write(""+section+"" + "<br>"); • </script> • </body> • </html>
  • 6. JavaScript Variables • As with algebra, JavaScript variables can be used to hold values (x=5) or expressions (z=x+y). • Variable can have short names (like x and y) or more descriptive names (age, sum, totalvolume). • Variable names must begin with a letter • Variable names can also begin with $ and _ (but we will not use it) • Variable names are case sensitive (y and Y are different variables) • Both JavaScript statements and JavaScript variables are case-sensitive.
  • 7. JavaScript Data Types • JavaScript variables can also hold other types of data, like text values (name="John Doe"). • In JavaScript a text like "John Doe" is called a string. • There are many types of JavaScript variables, but for now, just think of numbers and strings. • When you assign a text value to a variable, put double or single quotes around the value. • When you assign a numeric value to a variable, do not put quotes around the value. If you put quotes around a numeric value, it will be treated as text. • Example var pi=3.14; var name="John Doe"; var answer='Yes I am!';
  • 8. One Statement, Many Variables • You can declare many variables in one statement. Just start the statement with var and separate the variables by comma: • var name="Doe", age=30, job="carpenter"; • Your declaration can also span multiple lines: • var name="Doe", age=30, job="carpenter";
  • 9. JavaScript parseInt() Function • Definition and Usage --------The parseInt() function parses a string and returns an integer. • Note: Only the first number in the string is returned! • Note: Leading and trailing spaces are allowed. • Note: If the first character cannot be converted to a number, parseInt() returns NaN.
  • 10. EXAMPLE OUTPUT <script> document.write(parseInt("10") + "<br>"); 10 document.write(parseInt("10.33") + "<br>"); 10 document.write(parseInt("34 45 66") + "<br>"); 34 document.write(parseInt(" 60 ") + "<br>"); 60 document.write(parseInt("40 years") + "<br>"); 40 document.write(parseInt("He was 40") + "<br>"); NaN </script>
  • 11. JavaScript round() Method Definition and Usage • The round() method rounds a number to the nearest integer. Syntax Method var x= Math.round(10.53) var y= Math.round(8/5) Value to be rounded off var z= Math.round(mass/volume)
  • 12. More Examples Example OUTPUT var a=Math.round(2.60); 3 var b=Math.round(2.50); 3 var c=Math.round(2.49); 2 var d=Math.round(-2.60); -3 var e=Math.round(-2.50); -2 var f=Math.round(-2.49); -2
  • 13. Write a JavaScript source code for this problem. Use either alert, prompt, and document. write. 1. In Math, Jesus got 96 for the first grading, and 95 for the 2nd grading, 90 for the 3rd, and 97 for the the 4th grading. What will be his final grade in Math. Note: to find the final grade you have to 1. Add first all the grade from first – fourth grading 2. To get the final grade = sum or total of all the grades divided by 4