JavaScript Practice 03

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: Set a variable country to value “USA”. Then display an output box with 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

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

programmingtasks

1. TEST SCORER.1: Write a program that takes 3 test scores (max 100) and displays average score
R1-1) Prompt user to enter each test score this way – “Enter Test Score #1 (0 – 100):”
R1-2) After receiving all test scores compute the average – average = (sum of test scores) / (number of tests)
R1-3) Display the average test score this way – “Your average test score is: <AVERAGESCORE>”
NOTE: Comment your program by providing program name, developer and small description at the beginning

2. CLIMATE TRACKER: Write a program to display the sea level rise in given years for a given rate of yearly rise
R2-1) Ask the rate of Annual Rise: “Enter Rate of Annual rise (millimeters):”
R2-2) Ask number of years to calculate: “Enter Number of Years to check Rise Level:”
R2-3) Ocean Level Rise = Amount of yearly rise x Number of years
R2-4) Display total sea level rise: “Sea level will rise <Oceal Level Rise> millimeters in <Number of year>”
NOTE: Use a string variable for common words like millimeter so you don’t have to retype the literal value every time
NOTE: Test your program for Ocean Level Rise in 5,7 and 10 years
NOTE: Comment your program by providing program name, developer and small description at the beginning

3. BATTING AVERAGE CHECKER: Write a program to display batting average for a baseball player
R3-1) Get the player’s name – “Please Enter Player’s Name:”
R3-2) Get the number of hits – “Enter the player’s number of hits:”
R3-3) Get number of times at bat: “Enter the player’s number of times at bat.”
R3-4) battingAverage = hits / atBat
R3-5) display batting average: “Batting average of <PLAYERNAME> is: <battingAverage>”
NOTE: Comment your program by providing program name, developer and small description at the beginning

Leave a Reply

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