Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 138 of 589
Formatting text to add new lines in JavaScript and form like a table?
To format text with new lines in JavaScript and create table-like output, use the map() method combined with join(''). The '' character creates line breaks in console output. Syntax array.map(element => `formatted string`).join('') Example: Creating a Table-like Format let studentDetails = [ [101, 'John', 'JavaScript'], [102, 'Bob', 'MySQL'], [103, 'Alice', 'Python'] ]; // Create header let tableHeader = '||Id||Name||Subject||'; // Format data rows let tableRows = studentDetails.map(student => `|${student.join('|')}|` ).join(''); // Combine header ...
Read MoreIs there any more efficient way to code this "2 Sum" Questions JavaScript
Our job is to write a function that solves the two-sum problem in at most linear time. Two Sum Problem Given an array of integers, we have to find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers that add up to the target, and if no two elements add up to the target, our function should return an empty array. Brute Force Approach - O(n²) The naive approach uses nested loops to check every pair of elements: const bruteForceTwoSum ...
Read MoreCreating a JavaScript Object from Single Array and Defining the Key Value?
JavaScript provides several ways to create objects from arrays and define key-value pairs. This is useful when transforming data structures or converting between different formats. Using Object.entries() with map() The most common approach uses Object.entries() to convert an object into an array of key-value pairs, then map() to transform the structure: var studentObject = { 101: "John", 102: "David", 103: "Bob" }; var studentDetails = Object.entries(studentObject).map(([studentId, studentName]) => ({ studentId, studentName })); console.log(studentDetails); ...
Read MoreConverting string to MORSE code in JavaScript
What is Morse Code? Morse code is a method used in telecommunications to encode text characters as standardized sequences of two different signal durations, called dots and dashes. Each letter of the alphabet has a unique pattern of dots (.) and dashes (-). To convert a string to Morse code in JavaScript, we need an object that maps all alphabets to their Morse code equivalents, then iterate through the input string to build the encoded result. Morse Code Map First, let's create an object containing all alphabet-to-Morse mappings: const morseCode = { ...
Read MoreAssign multiple variables to the same value in JavaScript?
JavaScript allows you to assign the same value to multiple variables in a single statement using the assignment operator chaining technique. Syntax var variable1, variable2, variable3; variable1 = variable2 = variable3 = value; You can also declare and assign in one line: var variable1 = variable2 = variable3 = value; Example: Assigning Same Value to Multiple Variables var first, second, third, fourth, fifth; first = second = third = fourth = fifth = 100; console.log("first:", first); console.log("second:", second); console.log("third:", third); console.log("fourth:", fourth); console.log("fifth:", fifth); console.log("Sum of all values:", ...
Read MoreReturn the largest array between arrays JavaScript
We have an array of arrays that contains some numbers, we have to write a function that takes in that array and returns the index of the subarray that has the maximum sum. If more than one subarray has the same maximum sum, we have to return the index of first such subarray. Problem Overview Given multiple arrays nested within a main array, we need to: Calculate the sum of each subarray Find which subarray has the largest sum Return the index of that subarray ...
Read MoreCreating an associative array in JavaScript?
In JavaScript, there's no true associative array like in other languages. Instead, you use objects or arrays of objects to achieve similar functionality. JavaScript objects act as associative arrays where you can use string keys to access values. What are Associative Arrays? Associative arrays use named keys instead of numeric indexes. In JavaScript, regular arrays have numeric indexes, but objects provide key-value pairs that function like associative arrays. Method 1: Using Objects The most common approach is using plain JavaScript objects: // Creating an associative array using object var customer = { ...
Read MoreCreate a Calculator function in JavaScript
We have to write a function, say calculator() that takes in one of the four characters (+, - , *, / ) as the first argument and any number of Number literals after that. Our job is to perform the operation specified as the first argument over those numbers and return the result. If the operation is multiplication or addition, we are required to perform the same operation with every element. But if the operation is subtraction or division, we have to consider the first element as neutral and subtract all other elements from it or divide it by ...
Read MoreCreating an associative array in JavaScript with push()?
An associative array in JavaScript is essentially an object that uses string keys to store arrays of values. You can create this structure by combining forEach() loops with the push() method to group related data. Example: Grouping Students by ID Here's how to create an associative array that groups student names by their student ID: var studentDetails = [ { studentId: 1, studentName: "John" }, { ...
Read MoreFilter away object in array with null values JavaScript
When working with arrays of objects, you often need to filter out objects with invalid or empty values. This is common when processing API responses or user data that may contain incomplete records. Let's say we have an array of employee objects, but some have empty strings, null, or undefined values for the name field. We need to filter out these invalid entries. Sample Data Here's our employee data with some invalid entries: let data = [{ "name": "Ramesh Dhiman", "age": 67, "experience": ...
Read More