Front End Technology Articles

Page 182 of 652

Converting whitespace string to url in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 729 Views

In web URLs, browsers automatically replace spaces with '%20' for proper encoding. JavaScript provides multiple methods to convert whitespace characters to URL-encoded format. We need to write a function that takes a string and replaces all whitespace characters with '%20'. For example, if the input string is: const str = 'some extra Space'; Then the output should be: const output = 'some%20extra%20%20Space'; Using Manual Loop Method This approach iterates through each character and manually replaces spaces: const str = 'some extra Space'; const replaceWhitespace = (str = ...

Read More

How to allocate memory to an object whose length is set to 0 - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 148 Views

In JavaScript, you can manipulate array length to clear memory and allocate new slots. When you set an array's length to 0, it clears all elements. Setting length to a larger value allocates empty slots. Understanding Array Length Property The length property controls how many elements an array can hold. Setting it to 0 removes all elements, while setting it to a larger number creates empty slots filled with undefined. var arrayObject = [ "John", "David", "Mike" ]; console.log("Original array:", arrayObject); console.log("Original ...

Read More

Finding elements whose successors and predecessors are in array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 525 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. The function should construct and return a new array that contains all such elements from the original array whose successor and predecessor are both present in the array. This means, if any element num is in the original array, it should be included in the result array if and only if num - 1 and num + 1 are also present in the array. For example, if the input array is: const arr = ...

Read More

Fetch specific values from array of objects in JavaScript?

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

Fetching specific values from an array of objects is a common JavaScript task. This article demonstrates multiple approaches to filter and extract data based on specific criteria. Sample Data Let's start with an array of employee objects: const details = [ { employeeFirstName: "John", employeeLastName: "Doe" }, { employeeFirstName: "David", employeeLastName: "Miller" ...

Read More

Finding confusing number within an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 331 Views

A number in an array is confusing if it becomes another number which is also present in the array after we rotate it by 180 degrees. When rotated, digits transform as follows: 0→0, 1→1, 6→9, 8→8, 9→6. Other digits (2, 3, 4, 5, 7) cannot be rotated to form valid numbers. Understanding Confusing Numbers For a number to be confusing: It must contain only the digits 0, 1, 6, 8, 9 When rotated 180 degrees, it must form a valid number within our range The rotated number must also be present in the array Example ...

Read More

Enter values with prompt and evaluate on the basis of conditions in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 303 Views

JavaScript's prompt() function allows you to collect user input and evaluate it using conditional statements. This is useful for creating interactive web applications that respond based on user-provided values. Basic Syntax To get user input and convert it to a number: var value = parseInt(prompt("Enter a value")); Example: Conditional Evaluation Based on User Input Here's a complete example that prompts for two values and evaluates their product: Prompt and Evaluate ...

Read More

Checking digit sum of smallest number in the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 283 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument. The function should first pick the smallest number from the array and then calculate the sum of all the digits of the number. If the digit sum of that number is even, we should return true, false otherwise. Problem Example If the input array is: const arr = [12, 657, 23, 56, 34, 678, 42]; Then the output should be: const output = false; Because the smallest ...

Read More

Remove/ filter duplicate records from array - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 325 Views

JavaScript arrays often contain duplicate records that need to be removed. There are several effective methods to filter duplicates, depending on whether you're working with primitive values or objects. Sample Data with Duplicates Let's start with an array of nationality objects containing duplicate values: var objectOfNationality = [ { nationality: "Indian" }, { nationality: "American" }, { nationality: "Emirati" }, { nationality: "Indian" }, { nationality: "American" } ]; console.log("Original array:", objectOfNationality); ...

Read More

Finding the maximum number using at most one swap in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 303 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The task of our function is to perform at most one swap between any two digits of the number and yield the maximum possible number. If the number is already the maximum possible number, we should return the number itself. For example, if the input number is: const num = 1625; Then the output should be: const output = 6125; We swapped 1 and 6, and this is the only ...

Read More

Number prime test in JavaScript by creating a custom function?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 229 Views

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In JavaScript, we can create a custom function to check if a number is prime. Basic Prime Number Function Here's a simple approach to check if a number is prime: function checkNumberIsPrime(number) { if (number

Read More
Showing 1811–1820 of 6,519 articles
« Prev 1 180 181 182 183 184 652 Next »
Advertisements