1. READING IN USER INPUT AND
DISPLAYING IT ON THE SCREEN
JavaScript Variable
2. JAVASCRIPT TERMS/RULES TO
REMEMBER
• A JavaScript command used to display the
exact text of an statement.
• It is used to alert the user for some important
message.
• It is used if you want the user to accept
something.
• It is a symbol or other character indicating an
operation that acts on one or more elements.
• It is used to hold values and expressions.
3. JAVASCRIPT TERMS/RULES TO
REMEMBER
• = (equal sign) It a symbol used to assign values.
• The parseFloat() function parses a string and returns a
floating point number.
• This function determines if the first character in the
specified string is a number. If it is, it parses the string
until it reaches the end of the number, and returns the
number as a number, not as a string.
• Note: Only the first number in the string is returned! (“43” and
“88”) only the “43” will be converted.
• Note: Leading and trailing spaces are allowed.
• Note: If the first character cannot be converted to a number,
parseFloat() returns NaN. (“Age 40”) 40 will not be converted into
number because the 1st character is Age, so it will return as NaN
4. JAVASCRIPT TERMS/RULES TO
REMEMBER
• Statement is enclosed in an () parenthesis and a
quotation marks “”.
• (“Answer“+variablename+” is”) – used to get the
value of the variable. (with statement)
• (“”+variablename+””)- without statement.
• prompt(“Enter number “, ” “)- “, ” means nothing.
• (“nstatement” ,parseInt(variable name)- used
this to display the result to whole numbers
(Integers) ); but not round off
• (“ ”,parseInt(variable name));- used if no
statement
5. Example of source code with parseInt
function
• <html>
• <body>
• <script>
• var x= 74
• var y= 45
• var z= x/y
• alert(""+z+"");
• document.write("",parseInt(z));
• </script>
• </body>
• </html>
Web browser result alert box
Document.write result using
parseInt function = 1
6. EXERCISES
• Write the JavaScript code.
1. A 100 N (force) barbell is lifted to a height of
2 meters (distance). How much (work) is
done on the barbell?.
• Given:
• force = “100 N”
• distance= “2 meters”
• work= force * distance
• Directly on the page using document.write
• or Display on the alert box or a prompt box
7. Directly on the page using document.write
<html>
<head><title>JS</title>
</head>
<body>
<script>
var force= parseFloat("100 N")
var distance= parseFloat("2 meters")
var work= force * distance
document.write("The amount of work done on the barbell
is"+work+"Joules");
</script>
</body>
</html>
10. Using prompt box and document.write
<html>
<head><title>JS</title></head>
<body><script>
var force= parseFloat(prompt("Enter force"," "));
document.write("Force= "+force+" N");
var distance= parseFloat(prompt("Enter distance"," "));
document.write("Distance= "+distance+" meters");
var work= force * distance
alert("The amount of work done on the barbell is"+work+" Joules");
document.write("Work= "+work+" meters");
</script></body></html>
11. COMPUQUIZ
Write the JavaScript source code. Display the
result using either document write, alert box
or prompt box.
A. Determine the density at a 50 cubic
centimeter (volume), 500 gram gold bar
(mass).
(density) is equal to mass divided by volume.
12. COMPUQUIZ
B. Write the source code to get the following
users data using a prompt box and document
write.
• Name
• Age
• Birthday
• Address
• Contact Number
13. Assignment
Prepare this using intermediate paper or bond paper.
• 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
14. Assignment
2. Using JavaScript write the source code to get
the users data below using prompt box , alert
box and document.write.
• Name
• School
• Year Level
• Section