JavaScript Practice 04


A) Use Firefox Browser > Firebug > Console to write your javascript program and click ‘Run’ to execute
B) INPUT: Javascript syntax example for asking Input from user
* Example: Display an input box to user with the message “Please enter your name”. Store the entered value in the variable ‘person’
var person = prompt(“Please enter your name”, “”);
C) OUTPUT: Javascript syntax example for showing Output
* Example: Set a variable country to value “USA”. Then display an output box with message “Go !!!”.
country = “USA”;
alert(“Go ” + country + “!!!”);

D) DATA TYPE: Javascript allows declaring and initializing variables with or without a data type as shown above.
You can also “implicitly” define data type by initialization. Explicit data types – Number, Boolean, String.
* Examples:
var name; //Declare a variable without specifying data type
var lastname = “Smith”; //Initialize a string variable without specying data type (implcitly)
var total = 1.3; //Initialize a float (decimal) implicitly
var year = 23; //Initialize an integer implicitly
var greeting = new String(“Hello Friend”); //Initialize a String explicitly

E) COMMENTS: Line Comments in javascript are preceded by the // sign . Block Comments in javascript are enclosed with /* before and */ afterwards
* Example E.1 : //This is a line comment
* Example E.2 :
Program Title:
Developer Name:

F) MODULES: Javascript creates modules as follows:-
i) the keyword ‘function’
ii) followed by the module-name
iii) a parenthesis for parameters () and
iv) curley brace { and } to enclose the module code
* Example: Following code defines two functions – “greeting” and “goodbye”. They are called by the program upon start and exit
//function definition
function greeting(){
alert("Welcome User!");
function goodbye(){
alert("See ya later!!");
//main program
var person = prompt("Please enter your name", "");


1. NOTIFY.1: Write a program that displays an announcement, then calls a module to show a message, and finally shows an exit message
R1-1) Display to the user the announcement “I have a message for you”
R1-2) Call a module that will display the message “Hello World”
R1-3) Display the exit message “That’s all, folks!””
NOTE: In Javascript you MUST define your module (function) first before calling it (see Example #F above)

2. USER GUIDE: Write a program to display User Guide for ACME Dryer uninstallation
R2-1) Define 6 modules : 1 for startingMessage,1 for continueMessage, and 4 for the four steps
R2-2) The module for starting Message should display: “This program tells you how to uninstall an ACME laundry dryer in 4 steps.”
R2-3) Define a variable for continue Message – “Press OK to continue”. (NOTE: Re-use this variable in all steps as a common ending line)
R2-4) The module for step 1 should display “Step 1: Unplug the dryer and move it away from the wall. Press OK to continue”.
R2-5) The module for step 2 should display “Step 1: Remove the 6 screws from the back of the dryer. Press OK to continue”.
R2-6) The module for step 3 should display “Step 1: Remove the dryer back panel. Press OK to continue”.
R2-7) The module for step 4 should display “Step 1: Pull the top of the dryer straight up. Press OK to continue”.
R2-8) The program should run in following order:

    – Display starting message by calling the startingMessage function
    – Display step 1 message by calling the step1 function
    – Display step 2 message by calling the step2 function
    – Display step 3 message by calling the step3 function
    – Display step 4 message by calling the step4 function

Leave a Reply

Your email address will not be published. Required fields are marked *