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
Front End Technology Articles
Page 204 of 652
Returning a decimal that have 1s in its binary form only at indices specified by array in JavaScript
Problem We are required to write a JavaScript function that takes in an array of unique non-negative integers. Our function should return a 32-bit integer such that the integer, in its binary representation, has 1 at only those indexes (counted from right) which are in the sequence. Example Following is the code − const arr = [1, 2, 0, 4]; const buildDecimal = (arr = []) => { const bitArr = Array(31).fill(0); let res = 0; arr.forEach(el => { ...
Read MoreIs there any way to check if there is a null value in an object or array in JavaScript?
Null values can be tricky to work with in JavaScript, but they are an important part of the language. In this article, we will discuss various ways you can check if there is a null value in an object or array in JavaScript. The null values display that no object value is present. It is intentionally set to show that a variable has been declared but has not yet been given a value. The primitive value is undefined, which is an unintended absence of any object value, which is comparable to null, that contrasts with the former. This ...
Read MoreAdd class name in different li by pure in JavaScript?
Adding class names to different list items (li elements) is a common task in JavaScript DOM manipulation. This can be achieved using various methods like forEach() with classList.add(), CSS selectors, or DOM traversal properties. Let's explore different approaches to add class names to list items using pure JavaScript. Method 1: Using forEach() with classList.add() The forEach() method executes a function for each array element, while classList.add() adds one or more CSS classes to an element. Syntax array.forEach(function(currentValue, index, arr), thisValue) element.classList.add('className') Example ...
Read MoreSet object property in an array true/false, whether the id matches with any id from another array of objects in JavaScript?
In JavaScript, you often need to compare two arrays of objects and set a property (like matches) to true or false based on whether their IDs match. This is useful for data processing, filtering, and UI state management. An object in JavaScript is a collection of key-value pairs that represent properties and their values. Arrays are ordered lists that can store multiple values, including objects, and are accessed using numeric indices starting from 0. Syntax Basic array syntax: const arrayName = [item1, item2, ...]; Method 1: Using reduce() and map() The reduce() ...
Read MoreIs it possible to change the value of the function parameter in JavaScript?
In this article we are going to find out whether it is possible to change the value of the function parameter in JavaScript. A function accepts certain values as parameters and later while calling this function we need to pass values for these parameters as arguments. Typically a function definition contains the name of the function, parameters (optional) and the body of the function. Syntax Following is the syntax for function parameters − function functionName(parameter1, parameter2, parameter3) { // Statements } Yes, it is possible to change the value of ...
Read MoreReturning a range or a number that specifies the square root of a number in JavaScript
We need to write a JavaScript function that takes an integer n and returns either the exact square root (if n is a perfect square) or a range indicating where the square root falls between two consecutive integers. Problem Requirements Return integer k if n is a perfect square, such that k * k == n Return a range (k, k+1) if n is not a perfect square, where k * k < n and n < (k+1) * (k+1) Solution Approach We use Math.sqrt() to calculate the ...
Read MoreGet the number of true/false values in an array using JavaScript?
In JavaScript, you can count true/false values in arrays using various methods. Here are several approaches to accomplish this task. Using filter() Method (Recommended) The most efficient approach is using the filter() method to count values based on conditions: let obj = [ { isMarried: true }, { isMarried: false }, { isMarried: true }, { isMarried: true }, { isMarried: false } ]; // Count true values let trueCount = obj.filter(item => item.isMarried === ...
Read MoreReturning the first number that equals its index in an array using JavaScript
Problem We need to write a JavaScript function that takes an array of numbers and returns the first number whose value equals its 0-based index position. This means we're looking for an element where array[i] === i. Example Following is the code − const arr = [9, 2, 1, 3, 6, 5]; const findFirstSimilar = (arr = []) => { for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(el === i){ ...
Read MoreES6/ECMA6 template literals not working in JavaScript?
Template literals are a powerful feature of ES6 JavaScript that use backticks (`) instead of quotes. However, they may not work due to browser compatibility, syntax errors, or incorrect usage. This article covers common issues with template literals and their solutions, helping you debug and properly implement this ES6 feature. What Are Template Literals? Template literals use backticks (`) instead of quotes and support multi-line strings and embedded expressions using ${expression} syntax. Syntax const str = `string value`; const interpolated = `Hello ${name}!`; Common Issues and Solutions Issue 1: Browser Compatibility ...
Read MoreCalculating value of a sequence based on some input using JavaScript
Problem We need to write a JavaScript function that takes a number n as input and calculates the value of a sequence term un. Given a sequence where: u1 = 0 and u2 = 2 Our function should find the value of un for which the following recurrence relation holds: 6unun+1 - 5unun+2 + un+1un+2 = 0 Understanding the Sequence From the recurrence relation, we can derive that this sequence follows the pattern un = 2n-1 for n ≥ 2, and u1 = 0. Example Implementation Here's the ...
Read More