Variables 2

  • 343 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
343
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
1
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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 ina. Fahrenheit to CelsiusF= ?C= (F-32)* 5/9b. Celsius to KelvinC= ?K= C + 273.15
  • 3. <html><body><script>var F= 25var C= (F-32)*5/9alert(""+C+"")document.write(""+C+"");document.write("",parseInt(C));</script><script>var fahrenheit= parseFloat(prompt("Enter Fahrenheit "," "));var celsius= (fahrenheit-32)*5/9alert(""+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>"); 10document.write(parseInt("10.33") + "<br>"); 10document.write(parseInt("34 45 66") + "<br>"); 34document.write(parseInt(" 60 ") + "<br>"); 60document.write(parseInt("40 years") + "<br>"); 40document.write(parseInt("He was 40") + "<br>"); NaN</script>
  • 11. JavaScript round() MethodDefinition and Usage• The round() method rounds a number to the nearest integer.Syntax Methodvar x= Math.round(10.53)var y= Math.round(8/5) Value to be rounded offvar z= Math.round(mass/volume)
  • 12. More ExamplesExample OUTPUTvar a=Math.round(2.60); 3var b=Math.round(2.50); 3var c=Math.round(2.49); 2var d=Math.round(-2.60); -3var e=Math.round(-2.50); -2var 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 to1. Add first all the grade from first – fourth grading2. To get the final grade = sum or total of all the grades divided by 4