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.
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