We are required to write a JavaScript function that takes in array of integers as the first and the only argument. Our function should find and return that smallest positive integer which is not present in the array. For example, if the input array is: const arr = [4, 2, -1, 0, 3, 9, 1, -5]; Then the output should be: 5 Because 1, 2, 3, 4 are already present in the array and 5 is the smallest positive integer absent from the array. Using Sequential Search Approach ... Read More
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 More
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 More
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 More
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 More
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 More
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 More
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 More
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 More
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 More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance