Javascript Articles

Page 165 of 534

Finding the height based on width and screen size ratio (width:height) in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 726 Views

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 More

Constructing a string based on character matrix and number array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 255 Views

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 More

Limiting string up to a specified length in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

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 More

Checking if decimals share at least two common 1 bits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 159 Views

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 More

Finding the first non-consecutive number in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 451 Views

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 More

Encrypting a string based on an algorithm in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 320 Views

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 More

Alternating sum of elements of a two-dimensional array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 409 Views

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

Summing numbers present in a string separated by spaces using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 692 Views

We are required to write a JavaScript function that takes in a string which has integers inside it separated by spaces. The task of our function is to convert each integer in the string into an integer and return their sum. Problem Given a string containing numbers separated by spaces, we need to: Split the string into individual number strings Convert each string to a number Calculate the sum of all numbers Method 1: Using split() and reduce() const str = '1 5 ...

Read More

All ways to divide array of strings into parts in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 333 Views

In JavaScript, dividing an array into two non-empty parts can be accomplished through various approaches. This tutorial explores different methods to generate all possible ways to split an array of strings into exactly two parts. Problem Statement We need to create a JavaScript function that takes an array of strings with at least two elements and returns all possible ways to divide it into two non-empty parts. For example, with the array ["az", "toto", "picaro", "zone", "kiwi"], we want to generate: (az) | (toto picaro zone kiwi) (az toto) | (picaro zone kiwi) ...

Read More

Finding the count of total upside down numbers in a range using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 293 Views

Upside Down Numbers Upside down numbers are special numbers that remain identical when rotated 180 degrees. Only certain digits can form upside down numbers: 0, 1, 6, 8, and 9, where 6 becomes 9 and vice versa when rotated. For example, 69 becomes 69 when rotated, and 8108 becomes 8018 (which is not the same, so it's not upside down). Problem Statement We need to write a JavaScript function that takes a range of two numbers and returns the count of all upside down numbers within that range. Understanding Valid Digits When rotated 180 ...

Read More
Showing 1641–1650 of 5,340 articles
« Prev 1 163 164 165 166 167 534 Next »
Advertisements