JavaScript Object - Exercises, Practice, Solution
This resource offers a total of 90 JavaScript Object problems for practice. It includes 18 main exercises, each accompanied by solutions, detailed explanations, and four related problems.
[An Editor is available at the bottom of the page to write and execute the scripts.]
1. List Object Properties
Write a JavaScript program to list the properties of a JavaScript object.
Sample object:
var student = {
name : "David Rayy",
sclass : "VI",
rollno : 12 };
Sample Output: name,sclass,rollno
2. Delete Property
Write a JavaScript program to delete the rollno property from the following object. Also print the object before or after deleting the property.
Sample object:
var student = {
name : "David Rayy",
sclass : "VI",
rollno : 12 };
3. Object Length
Write a JavaScript program to get the length of a JavaScript object.
Sample object :
var student = {
name : "David Rayy",
sclass : "VI",
rollno : 12 };
4. Display Reading Status
Write a JavaScript program to display the reading status (i.e. display book name, author name and reading status) of the following books.
var library = [
{
author: 'Bill Gates',
title: 'The Road Ahead',
readingStatus: true
},
{
author: 'Steve Jobs',
title: 'Walter Isaacson',
readingStatus: true
},
{
author: 'Suzanne Collins',
title: 'Mockingjay: The Final Book of The Hunger Games',
readingStatus: false
}];
5. Cylinder Volume
Write a JavaScript program to get the volume of a cylindrical with four decimal places using object classes.
Volume of a cylinder : V = πr2h
where r is the radius and h is the height of the cylinder.
6. Bubble Sort
Write a bubble sort algorithm in JavaScript.
Note : Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted,
Sample Data: [6,4,0, 3,-2,1]
Expected Output : [-2, 0, 1, 3, 4, 6]
7. String Subsets
Write a JavaScript program that returns a subset of a string.
Sample Data: dog
Expected Output: ["d", "do", "dog", "o", "og", "g"]
8. Create Clock
Write a JavaScript program to create a clock.
Note: The output will come every second.
Expected Console Output :
"14:37:42"
"14:37:43"
"14:37:44"
"14:37:45"
"14:37:46"
"14:37:47"
9. Circle Area & Perimeter
Write a JavaScript program to calculate circle area and perimeter.
Note : Create two methods to calculate the area and perimeter. The radius of the circle will be supplied by the user.
10. Sort Object Array
Write a JavaScript program to sort an array of JavaScript objects.
Sample Object :
var library = [
{
title: 'The Road Ahead',
author: 'Bill Gates',
libraryID: 1254
},
{
title: 'Walter Isaacson',
author: 'Steve Jobs',
libraryID: 4264
},
{
title: 'Mockingjay: The Final Book of The Hunger Games',
author: 'Suzanne Collins',
libraryID: 3245
}];
Expected Output:
[[object Object] {
author: "Walter Isaacson",
libraryID: 4264,
title: "Steve Jobs"
}, [object Object] {
author: "Suzanne Collins",
libraryID: 3245,
title: "Mockingjay: The Final Book of The Hunger Games"
}, [object Object] {
author: "The Road Ahead",
libraryID: 1254,
title: "Bill Gates"
}]
11. Print Object Methods
Write a JavaScript function to print all the methods in a JavaScript object.
Test Data :
console.log(all_properties(Array));
["length", "name", "arguments", "caller", "prototype", "isArray", "observe", "unobserve"]
12. Parse URL
Write a JavaScript function to parse an URL.
Click me to see the solution
13. Object Properties (Own & Inherited)
Write a JavaScript function to retrieve all the names of an object's own and inherited properties.
14. Retrieve Object Values
Write a JavaScript function to retrieve all the values of an object's properties.
15. Object to Key-Value Pairs
Write a JavaScript function to convert an object into a list of `[key, value]` pairs.
16. Swap Keys and Values
Write a JavaScript function to get a copy of the object where the keys become the values and the values are the keys.
17. Check Property Existence
Write a JavaScript function to check whether an object contains a given property.
18. Check DOM Element
Write a JavaScript function to check whether a given value is a DOM element.
More to Come !
* To run the code mouse over on Result panel and click on 'RERUN' button.*
Live Demo:
See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
