× Introduction Setup Environment Building blocks Built-in functions Data Types Strings Operators Conditional statements Loop statements Functions Arrays Understaing Objects Date Object Number Object Math Object String Object Window Location Navigator History DOM Basics Forms
   Programs
Basic Control Loops Functions Arrays Examples Projects Quick Ref.
   Exercises
Variables Data Types Operators Decision Loops Reeborg's World



JavaScript Basic Programs

1. Ask for the user’s first name and display the output message:

Hello, [First Name].

View     Try It

2. Ask for the user’s first name and then ask for their surname
and display the output message:

Hello, [First Name] [Surname].

View     Try It

3. Ask the user to enter two numbers. Add them together and display the answer as:

The total is [answer].

View     Try It

4. Ask the user to enter two numbers. Add them together and display the answer as:

[number1] + [number2] = [answer].

View     Try It

5. Ask the user for their name and their age. Add 1 to their age and display the output:

[Name] , next birthday, you will be [new age].

View     Try It

6.Generate users lucky number (0-9) and display the output:

Your lucky number is [lucky number].

View     Try It

6a. Generate a sum as follows:

[number1]+[number2]=[result]

View     Try It

7.Write a program that will ask for a number of days and then will show how many hours, minutes and seconds are in that number of days. and display the output:

In [days] there are [hours] hours, [minutes] minutes and [seconds] seconds.

View     Try It

8. Ask the user to enter Original price of a material and discount in percentage.
Calculate sale_price = original_price - discount amount;
discountAmount = orgPrice x (discount/100);
Display the output as follows:

Sale Price = [sale_price].

View     Try It

9. Ask the user to enter two numbers. Print the result for all arithmetic operations:


Addition: num1+num2
Subtraction: num1-num2
Multiplication:num1*num2
Division:num1/num2
Modulus:num1%num2

View     Try It

10. Ask the user to enter three test scores and assign them to the test1, test2, and test3 variables.
Calculate the average of the three scores and assign the result to the average variable [average = (test1 + test2 + test3) / 3.0]
Display the average as:

The average score is [answer].

View     Try It

11. Ask the user to enter temperature in Celsius and assign it to celsius variable.
Convert it in to fahrenheit = (9 / 5) * celsius + 32. Display the temperture in fahrenheit as:

The temperture in fahrenheit is [answer].

View     Try It

12. Ask the user to enter 4 subjects marks and assign them to the 4 variables.
Calculate the total marks and the average of the marks. [averageMarks = totalMarks / 4] Display the result as:

Your total marks are: [totalMarks] and Average of marks are: [averageMarks]

View     Try It

13. Ask the user to enter basic salary.
Calculate TA of 12% and DA of 10%. Display Gross salary
[ta = basisSalaty x 12/100],
[da = basisSalaty x 10/100],
[grossSalaty = basisSalaty + ta + da]:

Gross salary is [grossSalary].

View     Try It

14. Develop a Mileage calculation application based on start reading, end reading and petrol consumed.
[milage = (endReading - startReading)/petrolConsumed].

Milage is [milage] Kms per liter.

View     Try It

15. Develop a future value calculation application based on desired future , annual interest rate and number of years investment.
[present_value = future_value / (1.0 + rate)**years].

You will need to deposit this amount: [present_value].

View     Try It

16. Develop an average speed calculation application based on distance travelled and time.
[speed = distanceTravelled / timeOfTravel].

Average Speed is: [speed] km/hr.

View     Try It

17. Develop a BMI calculation application based on weight and height.
[bmi = weightKgs /(heightM * heightM)].

BMI is: [bmi].

View     Try It

18. Develop an Area of Rectangle calculation application based on length and bradth.
[area = length * breadth].

Area of the rectangle is: [area].

View     Try It

19. Develop a feet to meters convertion application based on input feet .
[meters = 0.305 * foots].

[foots] foots = [meters] meters

View     Try It

20. Develop a kilograms to pounds convertion application based on input kilograms .
[pound = 2.024 * kilo].

[kilo] kilograms = [pound] pounds

View     Try It