Object Oriented Programming Articles

Page 12 of 589

Mapping string to Numerals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 869 Views

We are required to write a JavaScript function that takes in a string. It should print out each number for every corresponding letter in the string. Letter to Number Mapping Each letter corresponds to its position in the alphabet: a = 1 b = 2 c = 3 d = 4 e = 5 ... y = 25 z = 26 Note: The function should remove any special characters and spaces, processing only alphabetic characters. Example Input and Output If the input is: "hello man" Then the output ...

Read More

Writing table of number in array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 177 Views

We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m. Therefore, let's write the code for this function − Example The code for this will be − const num1 = 4; const num2 = 6; const multiples = (num1, num2) => { const res = []; for(let i = num1; i { return Array.from({length: count}, (_, index) => base * (index + 1)); }; console.log(generateTable(7, 5)); console.log(generateTable(3, 8)); [ 7, 14, 21, 28, 35 ] [ 3, 6, 9, 12, 15, 18, 21, 24 ] Using a Simple For Loop Here's another straightforward approach using a basic for loop: function createMultiplicationTable(number, count) { const table = []; for (let i = 1; i

Read More

Chunking arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 211 Views

Chunking arrays in JavaScript means splitting a single array into smaller subarrays of a specified size. This is useful for pagination, data processing, and organizing information into manageable groups. For example, if we have an array of 7 elements and want chunks of size 2, the last chunk will contain only 1 element since 7 is not evenly divisible by 2. Input and Expected Output Given this input array: const arr = [1, 2, 3, 4, 5, 6, 7]; The expected output should be: [[1, 2], [3, 4], [5, 6], [7]] ...

Read More

Fetching odd appearance number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 172 Views

Given an array of integers, we need to find the element that appears an odd number of times. There will always be exactly one such element. We can solve this problem using different approaches. The sorting approach iterates through a sorted array to count occurrences, while XOR provides an elegant mathematical solution. Method 1: Using Sorting This approach sorts the array first, then iterates through it to count consecutive identical elements. const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOddSorting = ...

Read More

How can I merge properties of two JavaScript objects dynamically?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 307 Views

To merge properties of two JavaScript objects dynamically, you can use the spread operator ({...object1, ...object2}) or Object.assign(). Both methods create a new object containing properties from multiple source objects. Using Spread Operator (Recommended) The spread operator is the modern and most concise approach: var firstObject = { firstName: 'John', lastName: 'Smith' }; var secondObject = { countryName: 'US' }; var mergedObject = {...firstObject, ...secondObject}; console.log(mergedObject); { firstName: 'John', lastName: 'Smith', countryName: 'US' } Using Object.assign() ...

Read More

How to sort an array of integers correctly in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 472 Views

To sort an array of integers correctly in JavaScript, use the sort() method with a comparison function. Without a comparison function, sort() converts numbers to strings, causing incorrect ordering. The Problem with Default sort() JavaScript's default sort() method converts elements to strings and sorts alphabetically, which doesn't work correctly for numbers: var numbers = [10, 2, 100, 5]; console.log("Default sort:", numbers.sort()); Default sort: [ 10, 100, 2, 5 ] Correct Integer Sorting Use a comparison function that returns the difference between two numbers: var arrayOfIntegers = [67, 45, ...

Read More

Understanding the find() method to search for a specific record in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 546 Views

The find() method searches through an array and returns the first element that matches a specified condition. It's perfect for locating specific records in arrays of objects. Syntax array.find(callback(element, index, array)) Parameters callback - Function to test each element element - Current element being processed index (optional) - Index of current element array (optional) - The array being searched Return Value Returns the first element that satisfies the condition, or undefined if no element is found. Example: Finding a Student Record var students = [ ...

Read More

JavaScript map() is not saving the new elements?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 177 Views

The map() method creates a new array with transformed elements but doesn't modify the original array. A common mistake is not assigning the returned value from map() to a variable. The Problem: Not Saving map() Results map() returns a new array, so you must capture its return value: let numbers = [1, 2, 3]; // Wrong: map() result is ignored numbers.map(x => x * 2); console.log(numbers); // Original array unchanged // Correct: assign the result let doubled = numbers.map(x => x * 2); console.log(doubled); // New array with transformed values [ ...

Read More

How to recognize when to use : or = in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 328 Views

The colon (:) is used to define properties in objects, while the equal sign (=) is used to assign values to variables. Understanding when to use each is fundamental in JavaScript. Using Colon (:) in Objects The colon separates property names from their values when creating object literals: var studentDetails = { "studentId": 101, "studentName": "John", "studentSubjectName": "Javascript", "studentCountryName": "US" }; console.log(studentDetails); { studentId: 101, studentName: 'John', studentSubjectName: 'Javascript', ...

Read More

Is the !! (not not) operator in JavaScript equivalent to reverse process of not operator?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 297 Views

Yes, the !! (not not) operator is the reverse process of the ! (not) operator. The single ! converts a value to its opposite boolean, while !! converts any value to its boolean equivalent. How the Not Operator Works The single ! operator converts truthy values to false and falsy values to true: var flag = true; console.log("Original value:", flag); console.log("Single ! result:", !flag); Original value: true Single ! result: false How the Not Not Operator Works The !! operator applies ! twice, effectively converting any value to its boolean ...

Read More
Showing 111–120 of 5,881 articles
« Prev 1 10 11 12 13 14 589 Next »
Advertisements