Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

625 views

523 views

523 views

Published on

No Downloads

Total views

625

On SlideShare

0

From Embeds

0

Number of Embeds

3

Shares

0

Downloads

8

Comments

0

Likes

1

No embeds

No notes for slide

- 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

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment