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 263 of 801
Creating a Projectile class to calculate height horizontal distance and landing in JavaScript
We need to create a JavaScript class called Projectile that simulates projectile motion physics. This class calculates horizontal distance traveled by a projectile given initial conditions. Problem Statement Create a Projectile class that takes three parameters during initialization: Starting height (h0): 0 ≤ h0 < 200 Starting velocity (v0): 0 < v0 < 200 Launch angle (a): 0° < a < 90°, measured in degrees The class should include a horiz method that calculates horizontal distance traveled at time t. Physics Formula The ...
Read MoreRemoving n characters from a string in alphabetical order in JavaScript
We need to write a JavaScript function that removes a specified number of characters from a string in alphabetical order. This means removing all 'a' characters first, then 'b', then 'c', and so on until we've removed the desired count. Problem Statement Given a lowercase alphabet string and a number, remove characters from the string in alphabetical order (starting with 'a', then 'b', 'c', etc.) until the specified count is reached. Example Let's implement a function that removes characters alphabetically: const str = 'abascus'; const num = 4; const removeAlphabetically = (str = ...
Read MoreCreate palindrome by changing each character to neighboring character in JavaScript
We need to write a JavaScript function that determines if we can create a palindrome by changing each character to its neighboring character in the alphabet. Each character can only move one position forward or backward (except 'a' can only go to 'b' and 'z' can only go to 'y'). Problem Requirements The function must handle these constraints: Each character MUST be changed either to the one before or the one after in the alphabet "a" can only be changed to "b" and "z" to "y" Return true if at ...
Read MoreFinding the height based on width and screen size ratio (width:height) in JavaScript
We are required to write a JavaScript function that takes in the width of the screen as the first argument and the aspect ratio (w:h) as the second argument. Based on these two inputs our function should return the height of the screen. Understanding Aspect Ratios An aspect ratio defines the proportional relationship between width and height. Common ratios include 16:9 (widescreen), 4:3 (traditional), and 21:9 (ultrawide). The formula is: height = (width × ratio_height) / ratio_width. Example Following is the code − const ratio = '18:11'; const width = 2417; const findHeight ...
Read MoreConstructing a string based on character matrix and number array in JavaScript
We need to write a JavaScript function that takes an n × n matrix of string characters and an array of integers (positive and unique), then constructs a string from characters at the specified 1-based positions. Problem Understanding Given a character matrix, we flatten it into a single array and extract characters at positions specified by the number array (using 1-based indexing). Character Matrix: [ ['a', 'b', 'c', 'd'], ['o', 'f', 'r', 'g'], ['h', 'i', 'e', 'j'], ['k', 'l', 'm', 'n'] ] ...
Read MoreLimiting string up to a specified length in JavaScript
Truncating strings to a specified length is a common requirement in JavaScript applications, especially for displaying previews or fitting text within UI constraints. This article shows how to limit string length and append ellipsis when truncation occurs. Problem We need to write a JavaScript function that takes a string and a number. The function should return the truncated version of the string up to the given limit followed by "..." if the result is shorter than the original string. Otherwise, it should return the same string if nothing was truncated. Using String.slice() Method The most straightforward ...
Read MoreChecking if decimals share at least two common 1 bits in JavaScript
We need to write a JavaScript function that takes two numbers and returns true if they have at least two common 1 bits in their binary representations at the same positions. Problem Given two decimal numbers, we want to check if their binary representations share at least two 1 bits at the same index positions. For example, if we have numbers 10 (binary: 1010) and 15 (binary: 1111), we need to compare bit by bit and count matching 1s. Algorithm Approach The solution involves converting both numbers to binary strings, aligning them by removing extra leading ...
Read MoreFinding the first non-consecutive number in an array in JavaScript
In JavaScript, finding the first non-consecutive number in an array means identifying the first element that is not exactly one more than its previous element. This is useful for detecting gaps in sequential data. Problem Statement We need to write a JavaScript function that takes an array of numbers and returns the first element that breaks the consecutive sequence. The function should return the element that is not the natural successor (+1) of its previous element. Algorithm The approach is to iterate through the array and compare each element with its previous element. When we find ...
Read MoreEncrypting a string based on an algorithm in JavaScript
We are required to write a JavaScript function that takes in a string and encrypts it based on the following algorithm: The string contains only space separated words. We need to encrypt each word in the string using the following rules: The first letter needs to be converted to its ASCII code. The second letter needs to be switched with the last letter. ...
Read MoreAlternating sum of elements of a two-dimensional array using JavaScript
Problem We are required to write a JavaScript function that takes in a 2-Dimensional array of m X n order of numbers. For this array, our function should calculate the alternating sum where each element is multiplied by (-1)^(i+j), where i and j are the row and column indices respectively. The mathematical formula is: $\sum_{i=1}^m \sum_{j=1}^n (-1)^{i+j}a_{ij}$ How It Works The alternating sum works by applying a positive or negative sign to each element based on its position. When the sum of row index (i) and column index (j) is even, the element gets a positive sign. When ...
Read More