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
Javascript Articles
Page 244 of 534
Get key from value in JavaScript
In JavaScript, finding a key from its corresponding value in an object requires iterating through the object's key-value pairs. Unlike accessing values by keys (which is direct), reverse lookup needs a search operation. Understanding the Problem Given a JavaScript object and a value, we need to find the key that maps to that value. For example: { key1: 'value1', key2: 'value2', key3: 'value3' } If we search for 'value2', we should get 'key2' as the result. Using Object.entries() and find() The most efficient ...
Read MoreGiven an array of integers, find the pair of adjacent elements that has the largest product and return that product JavaScript
Finding the pair of adjacent elements with the largest product is a common array problem in JavaScript. This involves iterating through the array and comparing products of consecutive elements. Problem Understanding Given an array of integers, we need to find two adjacent elements whose product is maximum among all possible adjacent pairs. For example, in array [1, 2, 3, 4], the adjacent pairs are (1, 2), (2, 3), and (3, 4) with products 2, 6, and 12 respectively. Method 1: Using -Infinity for Initialization This approach initializes the maximum product with -Infinity to handle arrays with ...
Read MoreHow to write the factorial function with reduce and range in JavaScript?
In this tutorial, we'll learn how to calculate factorials using JavaScript's reduce() method combined with array generation techniques. This approach demonstrates functional programming concepts while solving a common mathematical problem. Understanding reduce() and range Functions The reduce() function processes an array and reduces it to a single value by applying a function to each element. It takes an accumulator (which stores the result) and the current array value as parameters. const nums = [1, 2, 3, 4, 5]; const sum = nums.reduce((acc, val) => acc + val, 0); console.log(sum); 15 For ...
Read MoreJavaScript - How to create nested unordered list based on nesting of array?
In this tutorial, we'll learn how to create nested unordered lists in HTML using JavaScript. We'll use recursion to traverse nested arrays and generate the corresponding HTML structure dynamically. Understanding the Problem We need to convert a nested array structure into an HTML unordered list where: String elements become list items Array elements become nested sublists The structure preserves the original nesting levels For example: Coffee ...
Read MoreJavaScript Count repeating letter
In this problem statement, our target is to count the repeating letter in the given string with the help of Javascript functionalities. So we can solve this problem with the help of loops and some built-in methods of Javascript. Logic for the given problem In the given problem statement we have to design a program to count the repeating letters in the given string. For implementing this task first we will create a blank object for count, array for repeated characters and another object for results. Then we will loop through every character in the string. So ...
Read MoreFinding Lucky Numbers in a Matrix in JavaScript
In the given problem statement we have to write a function to get the lucky numbers in a matrix with the help of Javascript. A lucky number is defined as an element that is the minimum value in its row and the maximum value in its column. Understanding the Problem Statement A lucky number in a matrix must satisfy two conditions: It is the minimum element in its row It is the maximum element in its column For example, in the matrix [[3, 7], [9, 11]], the number 7 ...
Read MoreMapping values to keys JavaScript
In JavaScript, mapping values to keys means creating associations between identifiers (keys) and their corresponding data (values). This is commonly achieved using objects, which store data in key-value pairs for efficient retrieval. Understanding the Problem Objects in JavaScript are data structures that store information as key-value pairs. Keys serve as identifiers to access their associated values. There are several ways to access these values, with dot notation and bracket notation being the most common approaches. For example, with an object const obj = {name: 'John', age: 30}, you can access the name using obj.name or obj['name']. ...
Read MoreMasking a string JavaScript
Masking a string means replacing certain characters with asterisks (*) or other symbols to hide sensitive information like passwords, credit card numbers, or personal data. In JavaScript, we can create a function to mask specific portions of a string while keeping the rest visible. Understanding String Masking String masking is commonly used in applications to protect sensitive data while still showing partial information for verification purposes. For example, displaying a credit card number as "1234-****-****-5678" or an email as "j***@example.com". Basic Masking Function Here's a function that masks characters between specified start and end positions: ...
Read MoreMaximum difference between a number JavaScript
In this problem statement, our task is to write a function for finding the maximum difference between two numbers in an array using JavaScript. We'll explore both a brute force approach and an optimized solution. Understanding the Problem We need to find the maximum difference between any two numbers in an array where the larger number appears after the smaller number in the array order. For example, in array [2, 3, 5, 6, 4, 1], the maximum difference is 4 (6 - 2 = 4), not 5 (6 - 1), because 1 comes after 6. Method 1: ...
Read MoreN consecutive odd numbers JavaScript
In this problem statement, our target is to print the n consecutive odd numbers and implement this problem with the help of Javascript functionalities. We can solve this problem using loops and array manipulation in JavaScript. Logic for the Given Problem In the given problem statement we have to design a program to generate a given number of consecutive odd numbers starting from 1. It will be done by creating an empty array to store the odd numbers and then using a loop which will run n times to add every odd number to the created array. ...
Read More