Access PropertyProblem StatementWrite a function `getName(obj)` that returns the `name` property of an object.Approach 1. Use dot notation: `obj.name`. Solution`function getName(obj) { return obj.name;
Update PropertyProblem StatementWrite a function `setAge(obj, age)` that updates the object's `age` property and returns the object.Approach 1. Assign the new value to `obj.age`. 2. Return the original object. Solution`function setAge(obj, age) { obj.age = age; return obj;
Delete PropertyProblem StatementWrite a function `removeProperty(obj, key)` that deletes the specified key from the object.Approach 1. Use the `delete` operator. Solution`function removeProperty(obj, key) { delete obj[key]; return obj;
Property ShorthandProblem StatementWrite a function `createPerson(name, age)` that returns an object using ES6 property shorthand.Approach` 1. If key and variable name are same, just use the name once: `{ name, ageSolution`function createPerson(name, age) { return { name, age
Check if Property ExistsProblem StatementWrite a function `hasKey(obj, key)` that returns `true` if the key exists in the object.Approach 1. Use the `in` operator: `key in obj`. Solution`function hasKey(obj, key) { return key in obj;
Get All KeysProblem StatementWrite a function `getKeys(obj)` that returns an array of all keys in the object.Approach 1. Use `Object.keys(obj)`. Solution`function getKeys(obj) { return Object.keys(obj);
Merge ObjectsProblem StatementWrite a function `merge(obj1, obj2)` that returns a new object combining both using the spread operator.Approach` 1. Use `{...obj1, ...obj2Solution`function merge(obj1, obj2) { return { ...obj1, ...obj2
Object SizeProblem StatementWrite a function `getSize(obj)` that returns the number of properties in an object.Approach 1. Get keys array using `Object.keys()`. 2. Return its `length`. Solution`function getSize(obj) { return Object.keys(obj).length;
Optional ChainingProblem StatementWrite a function `getCity(user)` that returns `user.address.city` safely using optional chaining. Return `undefined` if it doesn't exist.Approach 1. Use `user?.address?.city`. Solution`function getCity(user) { return user?.address?.city;
Object MethodProblem StatementCreate an object `calculator` with a property `value` and a method `add(n)` that increments `value` using `this`.Approach 1. Use `this.value` inside the method. Solution`const calculator = { value: 0, add(n) { this.value += n;
Simple ClassProblem Statement`Define a class `Animal` that takes a `name` in the constructor and has a method `speak()` that returns "${nameApproach 1. Use `class` syntax. 2. Store `name` on `this.name`. Solution`class Animal { constructor(name) { this.name = name;
Class InheritanceProblem Statement`Extend `Animal` with a `Dog` class. Override `speak()` to return "${nameApproach 1. Use `extends`. 2. Don't forget `super(name)` if you define a constructor. (Or just override the method). Solution`class Dog extends Animal { speak() { return `${this.name
Immutable ObjectProblem StatementWrite a function `makeImmutable(obj)` that prevents any changes to `obj`.Approach 1. Use `Object.freeze(obj)`. Solution`function makeImmutable(obj) { return Object.freeze(obj);
Object to ArrayProblem StatementWrite a function `toPairs(obj)` that converts an object into an array of `[key, value]` pairs.Approach 1. Use `Object.entries(obj)`. Solution`function toPairs(obj) { return Object.entries(obj);
Array to ObjectProblem StatementWrite a function `fromPairs(pairs)` that converts an array of `[key, value]` pairs into an object.Approach 1. Use `Object.fromEntries(pairs)`. Solution`function fromPairs(pairs) { return Object.fromEntries(pairs);
Static MethodsProblem StatementAdd a static method `isDog(obj)` to the `Dog` class that returns `true` if the object is an instance of `Dog`.Approach 1. Use the `static` keyword. 2. Use the `instanceof` operator. Solution`class Dog { static isDog(obj) { return obj instanceof Dog;
Getter and SetterProblem StatementCreate a `Circle` class with a `radius` property. Use a getter to return the `area` (πr²).Approach` 1. Use `get area() { ...Solution`class Circle { constructor(radius) { this.radius = radius;
Object DestructuringProblem StatementWrite a function `greetUser({name, age})` that uses destructuring in the parameter to return `\"Hello ${name}, age ${age}\"`.Approach 1. Use curly braces in the function signature to destructure parameters. Solution`function greetUser({ name, age
Destructuring with RenameProblem StatementExtract `name` as `fullName` from an object using destructuring.Approach` 1. Syntax: `const { name: fullNameSolution`function getFullName(obj) { const { name: fullName
Prototype MethodProblem StatementAdd a `last()` method to `Array.prototype` that returns the last element of any array.Approach 1. Assign a function to `Array.prototype.last`. 2. Use `this` to access the array instance. Solution`Array.prototype.last = function() { return this[this.length - 1];