EXERCICES ON JavaScript PROGRAMMING LANGUAGE
Exercises on JavaScript Programming
1. Basics of JavaScript
Write a script that prints "Welcome to JavaScript!" in the browser console.
Create a program that declares variables of types string, number, and boolean,
assigns values to them, and prints their types using typeof.
Write a program that calculates the area of a rectangle given its length and width,
using values provided by the user through the prompt() function.
2. Conditional Statements
Write a program to check whether a number entered by the user is positive, negative,
or zero using an if-else statement.
Create a program that assigns a grade based on the student's score using a switch
statement (e.g., A for scores above 90, B for 80–89, etc.).
Use nested if-else statements to determine if a given year is a leap year.
3. Loops
Write a program to print numbers from 1 to 100, replacing multiples of 3 with "Fizz,"
multiples of 5 with "Buzz," and multiples of both with "FizzBuzz" (classic FizzBuzz
problem).
Create a program to display the first 10 Fibonacci numbers using a for loop.
Write a program using a while loop to calculate the sum of the digits of a number
entered by the user.
4. Functions
Write a function isPrime(n) that returns true if a number is prime and false
otherwise. Test the function with different inputs.
Create a function reverseString(str) that reverses a string and returns the result.
Implement a function that takes a number as an argument and returns its factorial
using recursion.
5. Arrays
Write a program that initializes an array with 10 random numbers and prints the
largest and smallest numbers in the array.
Create a program that sorts an array of strings in alphabetical order using the .sort()
method.
Write a program to merge two arrays into a single array and remove duplicate
elements.
6. Objects
Create an object called Car with properties like make, model, and year. Add a method
getDetails() that returns a string containing the car's details.
Write a program that creates a Student object with fields like name, age, and grades
(array). Add methods to calculate the average grade and display the student's
information.
Implement a simple inventory system using objects, where items have properties like
name, quantity, and price.
7. DOM Manipulation
Create an HTML file with a button. Write a JavaScript script to display an alert when
the button is clicked.
Write a script to change the background color of a webpage to a random color when a
button is pressed.
Create a form with input fields for name, email, and age. Write a JavaScript script to
validate the inputs when the form is submitted (e.g., check if the email is in a valid
format).
8. Event Handling
Write a program to display the current mouse coordinates on the screen as the user
moves the mouse.
Create a "counter" application with buttons to increment and decrement the counter,
and display the current value dynamically on the webpage.
Write a program to display a digital clock that updates every second.
9. Asynchronous JavaScript
Write a program to fetch data from a public API (e.g., JSONPlaceholder or
OpenWeatherMap) using the fetch() function and display it on the webpage.
Create a script to demonstrate the use of setTimeout() and setInterval() by
showing a countdown timer on the screen.
Write an async function that waits for a simulated API call (using setTimeout) and
logs the result to the console.
10. Additional Challenges
Implement a simple "To-Do List" application where users can add tasks, mark tasks as
complete, and remove tasks. Use HTML, CSS, and JavaScript for this.
Create a program to simulate a rock-paper-scissors game between the user and the
computer.
Write a program to visualize a bar chart based on an array of numbers using the DOM
and CSS.
Advanced Challenge
Build a JavaScript program that simulates a basic calculator (supporting addition,
subtraction, multiplication, and division) with a graphical user interface (GUI) using
HTML, CSS, and JavaScript.