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
Web Development Articles
Page 205 of 801
Return the nearest greater integer of the decimal number it is being called on in JavaScript
The Math.ceil() function returns the smallest integer greater than or equal to a given number. This means it "rounds up" to the nearest integer. Syntax Math.ceil(x) Parameters x: A number to round up to the nearest integer. Return Value Returns the smallest integer greater than or equal to the given number. If the input is not a number, returns NaN. Example: Basic Usage console.log(Math.ceil(4.3)); // 5 console.log(Math.ceil(4.8)); // 5 console.log(Math.ceil(5.0)); // 5 (already integer) console.log(Math.ceil(-2.3)); // -2 console.log(Math.ceil(-2.8)); // -2 ...
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 MoreFinding the only even or the only odd number in a string of space separated numbers in JavaScript
We are required to write a JavaScript function that takes in a string that contains numbers separated by spaces. The string either contains all odd numbers and only one even number or all even numbers and only one odd number. Our function should return that one different number from the string. Problem Given a string of space-separated numbers where all numbers are either odd or even except for one, we need to find and return that unique number that differs from the rest. Example Following is the code: const str = '2 4 ...
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 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 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 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 More