0% found this document useful (0 votes)
63 views

Tutorial J3: Name: Tan Wan Sean Date: 29 May 2020

The document provides instructions for creating basic JavaScript functions. It includes steps to create functions to display an alert with a user's car and salary, combine two strings, and call functions. It also explains the difference between displaying strings and numbers, and includes code to multiply two numbers using functions.

Uploaded by

hellloo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Tutorial J3: Name: Tan Wan Sean Date: 29 May 2020

The document provides instructions for creating basic JavaScript functions. It includes steps to create functions to display an alert with a user's car and salary, combine two strings, and call functions. It also explains the difference between displaying strings and numbers, and includes code to multiply two numbers using functions.

Uploaded by

hellloo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Tutorial J3

Aim: To learn about basic JavaScript and using functions

Name: Tan Wan Sean Date: 29 May 2020

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 function named car_cost() that takes two arguments, mycar and paycheck.

function car_cost(mycar, paycheck)

5. Create a window.alert() command that will display an alert with the following message:
“You have a <mycar variable here> and make $<paycheck variable here>”

window.alert("You have a " + mycar + " and make $" + paycheck);

6. Create a function named get_added_text() that returns the value of two strings added
together inside the function. The two strings to add are these two separate strings:

This project<space here>


is almost fun!

7. Write the function as follows:

function get_added_text()
{
var textpart1 = "This project ";
var textpart2 = "is almost fun!";
var addedtext = textpart1 + textpart2;
return addedtext;
}

8. In the main script (after the function definitions), call the car_cost() function, and send it the
values of “Proton” and 3500 as arguments.
car_cost("Proton", 3500);

9. In the main script (after the function definitions), assign the result of the get_added_text()
function to a variable named alerttext. Create an alert that pops up with the value of that
variable.

var alerttext=get_added_text();
window.alert(alerttext);

10. Save the file and execute it. When you open the Web page, you should see two alert messages:

You have a Proton and make $3500

This project is almost fun!

11. Which line calls the first window and which line calls the second window?.
window.alert("You have a "+ mycar + " and make $" + paycheck);
}
__________________________________________________________________________
var alerttext=get_added_text();
window.alert(alerttext);
__________________________________________________________________________

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

String variables may contain letters, numbers and other characters. Numeric


number contain only numbers and are suitable for numeric calculations such as addition
and multiplication. 
__________________________________________________________________________

13. Write the code below and run it:


14. Which document.write statement is actually working and why?
function display_HTML()
document.write(myheading + text ); is working because it calls out the function.

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

16. Using the above code write a program to get the radius of a circle and use a function to calculate
the circumference and the area of the circle.
17. Using the above code write a program to convert Fahrenheit to Celsius. The formula is
C = (F-32) * 5/9. Validate your program to check if 212F is converted to 100C. Show your code
below:

<!DOCTYPE html>

<html>

<body>

<p id="demo"></p>

<script>

function toCelsius(f){

return (5/9)*(f-32);

document.getElementById("demo").innerHTML=toCelsius(212);

</script>

</body>

</html>

You might also like