JavaScript Practice 02

resource

 

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 B.1: 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 C.1: Display an output box to user with the message “Go USA!!!”.
alert(“Go USA!!!”);
* Example C.2: Initialize a variable country to value “USA”. Then display an output box to user with the message “Go <countryname>!!!”.
country = “USA”;
alert(“Go ” + country + “!!!”);

D) DATA TYPE: Javascript has 3 data types – Number, Boolean, String
EXAMPLE:
var name; //Declare a variable wothout specifying data type
var lastname = “Smith”; //Initialize a string variable without specying data type
var rate = new Number(7.5); //Initialize a decimal number
var month = new Number(); //Declare an integer
var greeting = new String(“Hello Friend”); //Initialize a String
NOTE: Javascript allows declaring and initializing variables with or without a data type as shown above
E) Comments in javascript are preceded by the // sign

programmingtasks

1. TEXAN GREETER.1: Write a program that uses standard greeting to greet users. Following are the requirement.
R1-1) Always use the greeting ‘Howdy Partner’ when communication with user (input or output message)
R1-2) Ask the user their name with an input box this way: “Howdy Partner, what is your name?”
R1-3) After reading the user’s name, greet them with their name. For example if user entered ‘Bob’ display output: “Howdy Partner Bob!”

2. PAYCALC.1: Write a program to calculate weekly salary
R2-1) Assume current payrate is $7.75 per hour
R2-2) Ask the user how many hours they worked this week: “Enter number of hours worked this week:”
R2-3) weeklysalary = payrate * hours
R2-4) Display total salary this way: “Your salary for this week is: <SALARY>”
NOTE: <SALARY> is a place-holder for the actual salary.

3. PAYCALC.2: Write a program to calculate annual salary
R3-1) Assume current payrate is $7.75 per hour. Also there are 52 weeks in a year.
R3-2) Ask the user how many works they worked this week: “Enter number of hours worked this week:”
R3-3) weekly salary = payrate * hours
R3-4) annual salary = weekly salary * total weeks in a year
R3-5) Display total salary this way: “Your annual salary before taxes is: $<SALARY>”
NOTE: <SALARY> is a placeholder for the actual salary.
NOTE: Don’t forget to show the $ sign in the output to make it look nice!

Leave a Reply

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