0% found this document useful (0 votes)
86 views4 pages

Tutorial J4 Answer

The document is a tutorial on basic JavaScript and arithmetic operators. It contains instructions on creating an HTML page with script tags and using operators like addition, subtraction, multiplication, and modulus to manipulate a variable called paycheck. It also covers displaying strings versus numbers, functions, and operators like >=, <=, &&, and ||.

Uploaded by

hellloo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views4 pages

Tutorial J4 Answer

The document is a tutorial on basic JavaScript and arithmetic operators. It contains instructions on creating an HTML page with script tags and using operators like addition, subtraction, multiplication, and modulus to manipulate a variable called paycheck. It also covers displaying strings versus numbers, functions, and operators like >=, <=, &&, and ||.

Uploaded by

hellloo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Tutorial J4

Aim: To learn about basic JavaScript and using arithmetic operators


Name: Tan Wan Sean (SAM20012) Date:

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:

var paycheck = 2000;


document.write(paycheck + "<br>");

5. Use an operator to increase the value of paycheck to 4000 and display it:

paycheck += 2000;
document.write(paycheck + "<br>");

6. Use an operator to decrease the value of paycheck to 3500.

paycheck -= 500;
document.write(paycheck + "<br>");

7. Use an operator to decrease the value of paycheck to 0.

paycheck *= 0;
document.write(paycheck + "<br>");

8. Use an operator to increase the value of paycheck to 500.

paycheck += 500;
document.write(paycheck + "<br>");

9. Write the code to decrease the value of paycheck to 420.


paycheck -=80;
document.write (paycheck + "<br>");

10. Try out the modulus operator below:


Change x to 12, 13, 14, 15 and see the answers. How do you describe the answer?

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.

11. What is the difference to display a string and a numeric variable?

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.

12. Write the code below and run it:


<script>
function display_HTML() {
var myheading = "<h1>Hello, World!</h1>";
var mytext = "<p>While it is nice to know you world, there are
only some things that I am comfortable sharing in a global
context. You can't alter the variable that holds this text outside
of the function that contains it! Ha!</p>";
document.write(myheading + mytext);
}
display_HTML();
document.write(myheading + mytext);
</script>

13. Which document.write statement is actually working and why?

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>This example calls a function which performs a


calculation and returns the result:</p>

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

var rad = prompt("Please enter radius", "0");


var height = prompt("Please enter height", "0");
var x = myFunction(rad, height);
document.getElementById("demo").innerHTML = x;

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.

var num1 = 0, num2 = 1, num3 = 3, num4 = 5;

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");
}

You might also like