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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Greatest sum and smallest index difference in JavaScript
This problem asks us to find the maximum value of the expression (arr[i] + arr[j]) + (i - j) for any pair of indices in an array. Let's break down the solution step by step. Problem Statement Given an array of integers, we need to find an index pair (i, j) such that (arr[i] + arr[j]) + (i - j) is maximized. The function should return this maximum value. Understanding the Formula The expression (arr[i] + arr[j]) + (i - j) can be rearranged as: (arr[i] + i) + (arr[j] - j) ...
Read MoreChecking if all array values are smaller than a limit using JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers and a number. We should return true if all the numbers in the array are smaller than the number given as second argument, false otherwise. Using Array.every() Method The Array.every() method tests whether all elements in the array pass the test implemented by the provided function. It returns true if all elements satisfy the condition, false otherwise. Example 1: All Values Smaller Following is the code − const arr = [5, 34, 23, 14, 78, 45, 78]; const limit = ...
Read MoreHow to return true if the bottom of the page is visible using JavaScript?
In this tutorial, we will check if the bottom part of a page is visible or not to the user. We can do this functionality by using the height of the window and the height of the scrolled window. To write this code we need to understand three methods of JavaScript which are given as: scrollY − It is the read-only property of the window, and returns the pixels a document has scrolled vertically. window.scrollY scrollHeight − It is an HTML DOM element and a read-only property of the window. It returns the height of ...
Read MoreHow To Run JavaScript Online?
In this tutorial, we'll explore the best ways to run JavaScript code online without installing any software. Running JavaScript online is essential for quick testing, learning, and sharing code snippets. JavaScript can run in any web browser, but online editors provide enhanced features like syntax highlighting, error detection, and easy sharing. Let's examine the top platforms for running JavaScript online. Browser Console (Built-in Option) The simplest way to run JavaScript is using your browser's console. Press F12 to open Developer Tools, then navigate to the Console tab. console.log("Hello from browser console!"); let x = 5 ...
Read MoreJavaScript: lexical scoping issue using new keyword constructor while adding inner function?
When using constructor functions with inner functions, a common lexical scoping issue arises where the inner function cannot access the constructor's this context. This happens because inner functions have their own execution context. The Problem Inner functions don't inherit the this binding from their parent constructor, leading to undefined or incorrect values when trying to access constructor properties. function Employee() { this.technologyName = "JavaScript"; function workingTechnology() { // 'this' here refers to global object, not ...
Read MoreMake JavaScript take HTML input from user, parse and display?
JavaScript can retrieve HTML input values as strings and parse them into different data types. This tutorial shows how to get user input from HTML forms and convert it appropriately. Basic Input Retrieval HTML input elements return string values by default. Use document.getElementById() to access input values: Input Parser Example Parse Input function parseInput() { ...
Read MoreGo through an array and sum only numbers JavaScript
We are required to write a JavaScript function that takes in an array. The array might contain any type of value, Number literals, string literals, objects, undefined. Our function should pick all the Number type values and return their sum. Example const arr = [1, 2, 'a', 4]; const countNumbers = (arr = []) => { let sum = 0; for(let i = 0; i < arr.length; i++){ const el = arr[i]; ...
Read MorePossible combinations and convert into alphabet algorithm in JavaScript
In JavaScript, we can solve the alphabet decoding problem where each letter maps to a number (a=1, b=2, ..., z=26). Given an encoded message, we need to count how many different ways it can be decoded back into letters. For example, the message '111' can be decoded as 'aaa' (1-1-1), 'ka' (11-1), or 'ak' (1-11), giving us 3 possible combinations. Understanding the Problem The challenge is that some digit sequences can be interpreted in multiple ways: Single digits 1-9 can be decoded as letters a-i Two-digit combinations 10-26 can be decoded as letters j-z We ...
Read MoreSearching in a sorted 2-D array in JavaScript
We are required to write a JavaScript function that takes in an array of arrays of numbers as the first argument and a number as the second argument. The subarrays contain numbers sorted in an increasing order and no element of a preceding subarray is greater than any element of the succeeding subarray. The function should use the binary search algorithm to search for the element provided as the second argument in the sorted array of arrays. If the element exists the function should return true, false otherwise. For example − If the input array is ...
Read MoreImplement a custom function similar to Array.prototype.includes() method using JavaScript
We need to create a custom JavaScript function that mimics the behavior of Array.prototype.includes(). This function should check if a value exists in an array and return a boolean result. Problem We are required to write a JavaScript function that lives on the prototype object of Array. It must take in a literal value, and return true if that value is present in the array it is being called upon, false otherwise. Basic Implementation Here's a simple implementation using a for loop to iterate through the array: const arr = [1, 2, 3, 4, ...
Read More