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 380 of 652
How to pass arrays as function arguments in JavaScript?
In JavaScript, you can pass arrays as function arguments using two main approaches: the traditional apply() method and the modern ES6 spread operator (...). The spread operator is now the preferred approach as it provides cleaner, more readable code. Using apply() Method (Traditional) The apply() method was the traditional way to pass array elements as individual arguments to a function. It requires two parameters: the context (this value) and the array of arguments. Syntax functionName.apply(thisContext, arrayOfArguments) Example function displayMarkets(market1, market2, market3) { ...
Read MoreFind distinct elements - JavaScript
We are required to write a JavaScript function that takes in an array of literals, such that some array elements are repeated. We are required to return an array that contains elements that appear only once (not repeated). For example: If the array is: const arr = [9, 5, 6, 8, 7, 7, 1, 1, 1, 1, 1, 9, 8]; Then the output should be: const output = [5, 6]; Using indexOf() and lastIndexOf() The first approach uses indexOf() and lastIndexOf() to check if an element appears only once. If ...
Read MoreHow to find maximum value in an array using spread operator in JavaScript?
In this article, we will learn how to find the maximum value in an array using the spread operator with Math.max() in JavaScript. This approach is much cleaner and more efficient than passing array elements individually. What is the Spread Operator? The spread operator (...) expands an array or object into individual elements. It's perfect for passing array elements as separate arguments to functions like Math.max(). // Syntax Math.max(...arrayName) The Problem with Manual Approach Without the spread operator, you'd need to pass each array element individually to Math.max(), which becomes tedious for large ...
Read MoreDetermining full house in poker - JavaScript
A "full house" in poker is a hand containing three cards of one rank and two cards of another rank (e.g., three Kings and two Aces). We need to write a JavaScript function that checks if an array of five cards represents a full house. Understanding Full House A full house requires exactly: Three cards of the same rank (three of a kind) Two cards of another same rank (a pair) Example Implementation Here's a JavaScript function to detect a full house: const arr1 = ['K', 'K', 'K', 'A', 'A']; // Full ...
Read MoreHow to use spread operator to join two or more arrays in JavaScript?
The spread operator (...) provides a clean, modern way to join multiple arrays in JavaScript. It can be used for both immutable merging (creating new arrays) and mutable merging (modifying existing arrays). What is the Spread Operator? The spread operator (...) expands array elements, allowing you to copy values from one array to another. It performs a shallow copy of the original array. const mergedArray = [...array1, ...array2]; Method 1: Immutable Array Joining This approach creates a new array containing elements from all source arrays, leaving the original arrays unchanged. Joining Two ...
Read MoreMapping the letter of a string to an object of arrays - JavaScript
Given a string, we are required to write a function that creates an object that stores the indexes of each letter in an array. The letters (elements) of the string must be the keys of object. The indexes should be stored in an array and those arrays are values. For example, if the input string is: const str = 'cannot'; Then the output should be: const output = { 'c': [0], 'a': [1], 'n': [2, 3], ...
Read MoreIs 'floating-point arithmetic' 100% accurate in JavaScript?
In JavaScript, floating-point arithmetic is not 100% accurate due to how computers store decimal numbers in binary format. This leads to precision errors that can affect calculations involving decimal values. Understanding Floating Point Numbers A floating-point number is any number with a decimal point, such as 1.52, 0.14, or -98.345. In JavaScript, all numbers are stored using the IEEE 754 double precision format, which uses 64 bits to represent numbers. Sign Exponent (11 bits) Mantissa/Fraction (52 bits) ...
Read MoreHow to select a radio button by default in JavaScript?
Radio buttons allow users to select one option from multiple choices. Unlike checkboxes, only one radio button in a group can be selected at a time. To make a radio button selected by default, you can use the HTML checked attribute or JavaScript's checked property. Basic Radio Button Syntax Radio buttons are created using the HTML tag with type="radio": Option 1 Option 2 Method 1: Using HTML checked Attribute The simplest way to select a radio button by default is adding the checked attribute directly in HTML: ...
Read MoreFinding shared element between two strings - JavaScript
We are required to write a JavaScript function that takes in two strings that may / may not contain some common elements. The function should return an empty string if no common element exists otherwise a string containing all common elements between two strings. Following are our two strings − const str1 = 'Hey There!!, how are you'; const str2 = 'Can this be a special string'; Example Following is the code − const str1 = 'Hey There!!, how are you'; const str2 = 'Can this be a special string'; const ...
Read MoreHow to get the length of an object in JavaScript?
In JavaScript, objects don't have a built-in length property like arrays and strings. When you try to access object.length, it returns undefined. To get the number of properties in an object, you need to use specific methods. Why Objects Don't Have a Length Property The length property only works for arrays and strings, not plain objects: var object = {prop1: 1, prop2: 2}; document.write("Object length: " + object.length); Object length: undefined Arrays and Strings Have Length For comparison, ...
Read More