SlideShare une entreprise Scribd logo
1  sur  13
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

Contenu connexe

Tendances (20)

Monad presentation scala as a category
Monad presentation   scala as a categoryMonad presentation   scala as a category
Monad presentation scala as a category
 
Array
ArrayArray
Array
 
Modules 17 12_2020
Modules 17 12_2020Modules 17 12_2020
Modules 17 12_2020
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
Cs419 lec7 cfg
Cs419 lec7   cfgCs419 lec7   cfg
Cs419 lec7 cfg
 
Regexp
RegexpRegexp
Regexp
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
 
C programming , array 2020
C programming , array 2020C programming , array 2020
C programming , array 2020
 
week-7x
week-7xweek-7x
week-7x
 
Formal methods 3 - languages and machines
Formal methods   3 - languages and machinesFormal methods   3 - languages and machines
Formal methods 3 - languages and machines
 
Python : Regular expressions
Python : Regular expressionsPython : Regular expressions
Python : Regular expressions
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Head First Java Chapter 3
Head First Java Chapter 3Head First Java Chapter 3
Head First Java Chapter 3
 
Ch03
Ch03Ch03
Ch03
 
String in c programming
String in c programmingString in c programming
String in c programming
 
Lec4
Lec4Lec4
Lec4
 
Formal methods 5 - Pi calculus
Formal methods   5 - Pi calculusFormal methods   5 - Pi calculus
Formal methods 5 - Pi calculus
 
2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 

En vedette

En vedette (7)

Html web designing enhancing text
Html web designing enhancing textHtml web designing enhancing text
Html web designing enhancing text
 
Css margins
Css marginsCss margins
Css margins
 
Css In Iterations
Css In IterationsCss In Iterations
Css In Iterations
 
Css margin and padding property
Css margin and padding propertyCss margin and padding property
Css margin and padding property
 
Java script introduction
Java script introductionJava script introduction
Java script introduction
 
Lab#7 CSS Box Model
Lab#7 CSS Box ModelLab#7 CSS Box Model
Lab#7 CSS Box Model
 
CSS_Day_Three (W3schools)
CSS_Day_Three (W3schools)CSS_Day_Three (W3schools)
CSS_Day_Three (W3schools)
 

Similaire à Variables 2

C language first program
C language first programC language first program
C language first programNIKHIL KRISHNA
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overviewTAlha MAlik
 
Introduction to Python , Overview
Introduction to Python , OverviewIntroduction to Python , Overview
Introduction to Python , OverviewNB Veeresh
 
Lec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringLec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringSri Harsha Pamu
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)Saifur Rahman
 
Java script introducation & basics
Java script introducation & basicsJava script introducation & basics
Java script introducation & basicsH K
 
Java script
 Java script Java script
Java scriptbosybosy
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogramprincepavan
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogramprincepavan
 
C++ process new
C++ process newC++ process new
C++ process new敬倫 林
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview QuestionsGradeup
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and typesDaman Toor
 

Similaire à Variables 2 (20)

Variables
VariablesVariables
Variables
 
Get Fast C++ Homework Help
Get Fast C++ Homework HelpGet Fast C++ Homework Help
Get Fast C++ Homework Help
 
C language first program
C language first programC language first program
C language first program
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
Introduction to Python , Overview
Introduction to Python , OverviewIntroduction to Python , Overview
Introduction to Python , Overview
 
java tutorial 2
 java tutorial 2 java tutorial 2
java tutorial 2
 
Tut1
Tut1Tut1
Tut1
 
Lec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringLec04-CS110 Computational Engineering
Lec04-CS110 Computational Engineering
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
Python
PythonPython
Python
 
Java script introducation & basics
Java script introducation & basicsJava script introducation & basics
Java script introducation & basics
 
Java script
 Java script Java script
Java script
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
C++ process new
C++ process newC++ process new
C++ process new
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and types
 
CSC PPT 13.pptx
CSC PPT 13.pptxCSC PPT 13.pptx
CSC PPT 13.pptx
 

Plus de Jesus Obenita Jr.

Plus de Jesus Obenita Jr. (20)

Organization and management 3 a Evolution of Management Theory
Organization and management 3 a Evolution of Management TheoryOrganization and management 3 a Evolution of Management Theory
Organization and management 3 a Evolution of Management Theory
 
Organization and management 2 Management Function
Organization and management 2 Management FunctionOrganization and management 2 Management Function
Organization and management 2 Management Function
 
Organization and management 1
Organization and management 1Organization and management 1
Organization and management 1
 
Designing web page marquee and img tag
Designing web page  marquee and img tagDesigning web page  marquee and img tag
Designing web page marquee and img tag
 
Ms excel 2013 formatting worksheets
Ms excel 2013 formatting worksheetsMs excel 2013 formatting worksheets
Ms excel 2013 formatting worksheets
 
Ms excel 2013 data management
Ms excel 2013 data managementMs excel 2013 data management
Ms excel 2013 data management
 
Microsoft Excel introduction
Microsoft Excel introductionMicrosoft Excel introduction
Microsoft Excel introduction
 
Word 2013 working with pictures
Word 2013 working with picturesWord 2013 working with pictures
Word 2013 working with pictures
 
Word 2013 Formatting Page
Word 2013 Formatting PageWord 2013 Formatting Page
Word 2013 Formatting Page
 
Word 2013 8
Word 2013 8Word 2013 8
Word 2013 8
 
Ms word 2013 7
Ms word 2013 7Ms word 2013 7
Ms word 2013 7
 
Ms word 2013 6
Ms word 2013 6Ms word 2013 6
Ms word 2013 6
 
Ms word 2013 4
Ms word 2013 4Ms word 2013 4
Ms word 2013 4
 
Ms word 2013 2
Ms word 2013 2Ms word 2013 2
Ms word 2013 2
 
Ms word 2013
Ms word 2013Ms word 2013
Ms word 2013
 
Parts of the ms word 2013 screen and
Parts of the ms word 2013 screen andParts of the ms word 2013 screen and
Parts of the ms word 2013 screen and
 
Word processor
Word processorWord processor
Word processor
 
Session 2 test construction.mt's
Session 2   test construction.mt'sSession 2   test construction.mt's
Session 2 test construction.mt's
 
Cooking ingredients
Cooking ingredientsCooking ingredients
Cooking ingredients
 
Color theory
Color theoryColor theory
Color theory
 

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