Tutorial J4 Answer
Tutorial J4 Answer
1. Open a text editor of your choice, such as Notepad (on Windows) or TextEdit (on a Mac).
2. Create an HTML page leaving space between <body> and </body> tags.
3. Between the <body> and </body> tags add the <script> and </script> tags.
4. Create a variable named paycheck and give it an initial value of 2000 and display it:
5. Use an operator to increase the value of paycheck to 4000 and display it:
paycheck += 2000;
document.write(paycheck + "<br>");
paycheck -= 500;
document.write(paycheck + "<br>");
paycheck *= 0;
document.write(paycheck + "<br>");
paycheck += 500;
document.write(paycheck + "<br>");
When x= 11, it will show 1 on the webpage. Then it will increase by 1 when the x change to 12.
Thus, the number on the webpage will change to 2. Same thing happened when x=13 and x=14.
The webpage will show 3 when x=13 and 4 when x=14. When x=15 the webpage shows 0.
Numeric variables contain only numbers and are suitable for numeric calculations such as
addition and multiplication. String variables may contain letters, number and other characters.
you can’t do calculations on string variables even if they contain only numbers.
Document.write which is in the display function because it is written in the display function.i
14. Try the code below. The getElementById() method returns the element that has the ID attribute
with the specified value. The program multiplies a=4 and b=3.
<p id="demo"></p>
<script>
var x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;
function myFunction(a, b) {
return a * b;
}
</script>
15. Using the above code write a program to calculate the volume of a cylinder. You will need to ask
the user for the radius and height of the cylinder, The formula for volume is V=r2h.
<!DOCTYPE html>
<html>
<body>
<p>This example calls a function which performs a calculation and returns the result:</p>
<p id="demo"></p>
<script>
function myFunction(a, b) {
return Math.PI * a * a * b;
}
</script>
</body>
</html>
16. Write and test the script below. Append the code to compare num3 and num4 using the >=
operator and num2 and 3 using the <= operator. Also try the && and || operators. Write the
extra code below.
if (num1 == num2){
window.alert("True");
}
else{
window.alert("False");
}
if (num3 >= num4){
window.alert("True");
}
else{
window.alert("False");
}
if (num2 <= num3){
window.alert("True");
}
else{
window.alert("False");
}
if (num3 >= num4 && num2 <= num 3){
window.alert("True");
}
else{
window.alert("False");
}
if (num3 >= num4 || num2 <= num3){
window.alert("True");
}
else{
window.alert("False");
}