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 183 of 652
Finding longest line of 1s in a matrix in JavaScript
Suppose, we have a binary matrix (an array of arrays that contains only 0 or 1) like this: const arr = [ [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 1] ]; We need to write a JavaScript function that takes a binary matrix as input and finds the longest line of consecutive ones. The line can be horizontal, vertical, diagonal, or anti-diagonal. For the above array, the output should be 3 because the longest line starts from arr[0][1] and spans diagonally to ...
Read MoreComparing adjacent element and swap - JavaScript?
Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if they are in the wrong order. This process repeats until the array is sorted. How Bubble Sort Works The algorithm repeatedly steps through the array, compares adjacent elements, and swaps them if they are in the wrong order. Each pass "bubbles" the largest element to its correct position. Bubble Sort Process Initial: 10 30 5 ...
Read MoreChecking a Checkbox with JavaScript
In JavaScript, you can programmatically check a checkbox by setting its checked property to true. This is useful for form validation, user interactions, or initializing form states. HTML Structure First, let's look at a basic checkbox structure: John David Using the checked Property The checked property is a boolean that determines whether a checkbox is selected. Set it to true to check the checkbox, or false to uncheck it. Checkbox Example ...
Read MoreFinding the sum of two numbers without using '+', '-', '/', '*' in JavaScript
We are required to write a JavaScript function add() that takes in two numbers m and n. The function should, without using the four basic arithmetic operations add the two numbers taken as input and return the sum. The Challenge Without using +, -, *, or /, we need to implement addition using bitwise operations. This approach mimics how computers perform addition at the hardware level using binary logic. Example The code for this will be − const m = 67, n = 33; const add = (x, y) => { while(y !== 0){ let carry = x & y; x = x ^ y; y = carry
Read MoreDifference between two times using Dayjs JavaScript library?
Day.js is a lightweight JavaScript library for date manipulation. The diff() method calculates the difference between two time instances and returns the result in the specified unit. Syntax dayjs().diff(date, unit) Parameters date - The date to compare with unit - The unit of measurement: 'milliseconds', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years' Example: Basic Time Difference Day.js Time Difference ...
Read MoreFinding the longest substring uncommon in array in JavaScript
In JavaScript, finding the longest uncommon subsequence in an array involves identifying the longest string that appears only once or doesn't exist as a subsequence in other strings. Understanding Subsequence A subsequence is a sequence derived from another sequence by deleting some characters without changing the order of remaining elements. Any string is a subsequence of itself, and an empty string is a subsequence of any string. Problem Definition We need to find the length of the longest uncommon subsequence among an array of strings. An uncommon subsequence is a subsequence of one string that is ...
Read MoreWhat is the best way to reduce and merge a collection of objects – JavaScript?
The best way to reduce and merge a collection of objects is to use Object.values() combined with reduce() to group objects by a key and merge their properties. This approach is useful when you have duplicate objects and want to consolidate them, such as combining student records with the same ID. Example Data Consider this collection of student objects with duplicates: var details = [ { studentId: 10, marks: 75, studentName: "John" }, { studentId: 10, marks: 75, studentName: "John" }, { studentId: ...
Read MoreDeriving Random10() function from Random7() in JavaScript
Suppose we have a random7() function that generates random numbers from 1 to 7. We need to create a random10() function that generates random numbers from 1 to 10, using only the random7() function. Problem const random7 = () => Math.ceil(Math.random() * 7); This function yields a random number between 1 and 7 (inclusive) every time we call it. We need to write a random10() function that returns random numbers between 1 and 10 (inclusive) using only this random7() function. Using Rejection Sampling (Optimal Solution) The most efficient approach uses rejection sampling. We ...
Read MoreHow to convert a string with zeros to number in JavaScript?
When you have a string containing numbers with dots or zeros as separators, you can convert it to a number using JavaScript's built-in methods. This is commonly needed when processing formatted numeric data. The Problem Consider a string like "453.000.00.00.0000" where dots are used as thousand separators rather than decimal points. Direct conversion methods like Number() won't work correctly because JavaScript interprets the first dot as a decimal separator. let stringValue = "453.000.00.00.0000"; console.log("Original string:", stringValue); console.log("Direct Number() conversion:", Number(stringValue)); // NaN Original string: 453.000.00.00.0000 Direct Number() conversion: NaN Using parseInt() ...
Read MoreHow to make filter and word replacement method - JavaScript?
In JavaScript, there's no built-in method to replace all occurrences of a word in a string. You can create custom functions using methods like split() and join(), or use modern approaches like replaceAll(). Let's explore different methods to filter and replace words in a string: var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript"; Method 1: Using split() and join() This method splits the string by the target word and joins the parts with the replacement: var sentence = "Yes, My Name ...
Read More