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 216 of 801
Placing integers at correct index in JavaScript
We need to write a JavaScript function that takes a string containing only square brackets '[' and ']' and determines the minimum number of brackets to add to make it balanced. Problem Statement Given a string consisting of only '[' and ']' characters, find the minimum number of brackets that need to be added to make the string valid (properly balanced). A valid bracket string means every opening bracket '[' has a corresponding closing bracket ']' that comes after it. Example Input and Output Input: const str = '[]]'; Output: 1 ...
Read MoreChecking for straight lines in JavaScript
We need to write a JavaScript function that takes an array of coordinate pairs and determines if all points form a straight line. Each subarray contains exactly two items representing x and y coordinates. Our function checks whether the coordinates form a straight line by calculating and comparing slopes between points. For example: [[4, 5], [5, 6]] should return true (slope = 1) [[1, 2], [2, 3], [4, 5]] should return true (all have slope = 1) [[1, 1], [2, 2], [3, 4]] should return false (different slopes) The array is guaranteed to contain ...
Read MoreFinding score of brackets in JavaScript
We are required to write a JavaScript function that takes in a balanced square bracket string as an argument and computes its score based on specific rules. Scoring Rules The bracket scoring follows these rules: [] has score 1 AB has score A + B, where A and B are balanced bracket strings [A] has score 2 * A, where A is a balanced bracket string Example Input and Output For the input string '[][]': Input: '[][]' Output: 2 This works because [] scores 1, and [][] is two ...
Read MoreFinding the power of a string from a string with repeated letters in JavaScript
The power of a string is the maximum length of a non-empty substring that contains only one unique character. We are required to write a JavaScript function that takes in a string and returns its power. For example − const str = "abbcccddddeeeeedcba" Then the output should be 5, because the substring "eeeee" is of length 5 with the character 'e' only. How It Works The algorithm traverses the string and counts consecutive identical characters. It tracks the maximum count found, which represents the power of the string. ...
Read MoreFinding day of week from date (day, month, year) in JavaScript
We are required to write a JavaScript function that takes in three arguments, namely: day, month and year. Based on these three inputs, our function should find the day of the week on that date. For example: If the inputs are − day = 15, month = 8, year = 1993 Output Then the output should be − const output = 'Sunday' Using Built-in Date Object (Simple Method) The easiest approach is to use JavaScript's built-in Date object: function getDayOfWeek(day, month, year) { ...
Read MoreCount and return the number of characters of str1 that makes appearances in str2 using JavaScript
We need to write a JavaScript function that takes two strings as parameters and counts how many characters from the first string appear in the second string, including duplicate occurrences. Problem Statement Given two strings str1 and str2, count how many characters from str1 also appear in str2. If a character appears multiple times in str2, count each occurrence separately. Example: str1 = 'Kk' contains characters 'K' and 'k' str2 = 'klKKkKsl' contains: k(1), l(1), K(2), K(3), k(4), K(5), s(6), l(7) Characters from str1 found in str2: k, K, K, k, K = 5 matches ...
Read MoreHow to group an array of objects by key in JavaScript
Suppose, we have an array of objects containing data about some cars like this: const arr = [ { 'make': 'audi', 'model': 'r8', 'year': '2012' }, { 'make': 'audi', 'model': 'rs5', 'year': '2013' }, { ...
Read MoreRemove duplicates and map an array in JavaScript
When working with arrays of objects, a common task is extracting unique values from a specific property. This tutorial shows how to remove duplicates and map an array to extract unique "name" values from objects. Problem Statement Suppose we have an array of objects like this: const arr = [ {id: 123, value: "value1", name: "Name1"}, {id: 124, value: "value2", name: "Name1"}, {id: 125, value: "value3", name: "Name2"}, {id: 126, value: "value4", name: "Name2"} ]; Note that some ...
Read MoreConvert mixed case string to lower case in JavaScript
In JavaScript, there are multiple ways to convert a mixed-case string to lowercase. The most common approach is using the built-in toLowerCase() method, but you can also implement a custom solution using character codes. Using the Built-in toLowerCase() Method The simplest and most efficient way is to use JavaScript's built-in toLowerCase() method: const str = 'ABcD123'; const output = str.toLowerCase(); console.log(output); abcd123 Custom Implementation Using Character Codes For educational purposes, here's how to implement a custom convertToLower() function that converts uppercase letters (ASCII 65-90) to lowercase by adding 32 to ...
Read MoreFilter array of objects whose properties contains a value in JavaScript
Filtering an array of objects based on property values is a common task in JavaScript. This technique allows you to search through all properties of objects to find matches based on a keyword. Sample Data Let's start with an array of objects containing user information: const arr = [{ name: 'Paul', country: 'Canada', }, { name: 'Lea', country: 'Italy', }, { name: 'John', country: 'Italy', }]; The Problem We need to filter objects where any ...
Read More