× 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 Control Structures Programs

1. Ask the user to enter their age. If they are 18 or over, display output:

You are eligible to vote.

View     Try It

2. Ask the user to enter a number. Determine and display the number is odd or even.

[number], is even/odd.

View     Try It

3. Ask for two numbers. Determine and display the larger value:

The larger value is, [largerValue].

View     Try It

4.Ask the user to enter a number that is under 20. If they enter a number that is 20 or more, display the message "Too high", otherwise display "Thank you".

View     Try It

5.Ask the user to enter a number between 10 and 20 (inclusive). If they enter a number within this range, display the message "Thank you", otherwise display the message "Incorrect answer".

View     Try It

6.Ask the user to enter their favourite colour. If they enter "red", "RED" or "Red" display the message "I like red too", otherwise display the message "I don’t like [colour], I prefer red".

View     Try It

7.Ask the user’s age. If they are 18 or over, display the message "You can vote", If they are under 18, display how may year are left to eligible to vote.

View     Try It

8.Ask the user to enter a number. If it is under 10, display the message "Too low", if their number is between 10 and 20, display "Correct", otherwise display "Too high".

View     Try It

9.Ask the user to enter 1, 2 or 3. If they enter a 1, display the message "Thank you", if they enter a 2, display "Well done", if they enter a 3, display "Correct". If they enter anything else, display "Error message".

View     Try It

10.Ask the user to enter a number. Determine whether the number is Positive, Negative, or Zero.

View     Try It

11.Ask the user to enter any year. Determine whether the year is leaf year or not. [A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400]

View     Try It

12.Ask the users to enter their weight in Kilograms and height in meters. Find the BMI and display "Underweight" if BMI is less than 18.5, "Optimal" if BMI is between 18.5 and 25 and "Over weight" if BMI is greater that 25.

View     Try It

13.Bonus calculator: Ask the user to enter sales amount. If sales amout is greater than Rs 10000/- display bonus as Rs1000/- otherwise display bonus as Rs500/-

View     Try It

14. Salary calculator: Ask the user to enter sales amount. Calculate salary of 0.1% of sales and add bonus of Rs1500/-, if sales amout is greater than Rs 25000/- otherwise add bonus as Rs500/-

View     Try It

15. Discount calculator: Ask the user to enter purchase amount. Provide discount of 0.18% if the purchase amount is>4000, 0.15% if the purchase amount is>2500, 0.10% if the purchase amount is>1000 otherwise 0.05% and display final price.

View     Try It

16.Sum quiz (Using if statement): Generate two random numbers. Ask user to enter the sum of the two numbers. Check the user answer and display "Correct Answer", if it is correct.

View     Try It

17.Sum quiz (Using if-else statement): Generate two random numbers. Ask user to enter the sum of the two numbers. Check the user answer and display "Correct Answer", if it is correct, else diaply "Wrong answer".

View     Try It

18.Ask user to enter three numbers. Write a program to find the largest of three numbers. (nested if condition)

View     Try It

19.Ask user to enter three numbers. Write a program to find the largest of three numbers. (using logical operators)

View     Try It

20.develop a program to play a lottery. The program randomly generates a two-digit number, prompts the user to enter a two-digit number, and determines whether the user wins according to the following rules:
i.If the user’s input matches the lottery in the exact order, the award is Rs5,000.
ii.If all the digits in the user’s input match all the digits in the lottery number, the award is Rs3,000.
iii.If one digit in the user’s input matches a digit in the lottery number, the award is Rs1,000.

View     Try It

21. 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. If the student scores >75%, then display the grade as "Distinction". If the student scores >=60% and <75%, then display the grade as "First Class". If the student scores >=50% and <60%, then display the grade as "Second Class". If the student scores >=40% and <50%, then display the grade as "Third Class". If the student scores <40%, then display the grade as "No Class".

View     Try It