Found 6710 Articles for Javascript

JavaScript remove random item from array and then remove it from array until array is empty

Alshifa Hasnain
Updated on 13-Dec-2024 13:53:44

3K+ Views

We are given an array of string/number literals. We are required to create a function removeRandom() that takes in the array, recursively removes one random item from it, and simultaneously prints it until the array contains items. This can be done by creating a random number using Math.random(), removing the item at that index using Array.prototype.splice(), and printing it until the length of the array shrinks to 0. Approaches to remove random items from array Following are the different approaches to removing the random item from an array and then removing it from the array until ... Read More

JavaScript Count characters case insensitive

Disha Verma
Updated on 11-Mar-2025 16:38:27

568 Views

Counting characters in a string is a common task in programming, especially when analyzing text or creating applications that handle user input. Counting characters in a string without worrying about uppercase or lowercase letters is a common task in text processing and data analysis. JavaScript offers different methods to easily count how many times each character appears, regardless of case. Understanding Case Insensitive Case insensitivity in programming means that a system takes uppercase and lowercase letters as the same. For example, "Hello" and "hello" would be treated as equal in a case-insensitive situation. Case-Insensitive Character Counting You can ... Read More

Check if object contains all keys in JavaScript array

AmitDiwan
Updated on 19-Aug-2020 07:23:48

526 Views

We are required to write a function containsAll() that takes in two arguments, first an object and second an array of strings. It returns a boolean based on the fact whether or not the object contains all the properties that are mentioned as strings in the array.So, let’s write the code for this. We will iterate over the array, checking for the existence of each element in the object, if we found a string that’s not a key of object, we exit and return false, otherwise we return true.Here is the code for doing the same −Exampleconst obj = { ... Read More

JavaScript map value to keys (reverse object mapping)

Alshifa Hasnain
Updated on 24-Dec-2024 17:45:53

1K+ Views

In JavaScript, there are scenarios where we need to reverse the mapping of keys and values in an object, creating a new object where the original values become keys, and the keys become their corresponding values. For example, cities can be grouped by their states. In this article, we’ll learn two approaches to achieve this: using Object.keys with iteration and reduce. Iterating with Object.keys() The first approach involves iterating over the keys of the original object using Object.keys() of Object properties and populating a new object. If multiple keys in the original object share the same value, they are grouped into ... Read More

How to get the product of two integers without using * JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:21:19

322 Views

We are required to write a function that takes in two numbers and returns their product, but without using the (*) operator.Trick 1: Using Divide Operator TwiceWe know that multiplication and division are just the inverse of each other, so if we divide a number by other number’s inverse, won’t it be same as multiplying the two numbers?Let’s see the code for this −const a = 20, b = 45; const product = (a, b) => a / (1 / b); console.log(product(a, b));Trick 2: Using LogarithmsLet’s examine the properties of logarithms first −log(a) + log(b) = log(ab)So, let’s use this ... Read More

Remove number properties from an object JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:19:58

274 Views

We are given an object that contains some random properties, including some numbers, boolean, strings and the object itself.We are required to write a function that takes in the object as first argument and a string as second argument, possible value for second argument is a name of any data type in JavaScript like number, string, object, boolean, symbol etc.Our task is to delete every property of type specified by the second argument. If the second argument is not provided, take ‘number’ as default.The full code for doing so will be −const obj = {    name: 'Lokesh Rahul',   ... Read More

JavaScript reduce sum array with undefined values

AmitDiwan
Updated on 06-Dec-2024 01:41:22

885 Views

If you’re working with arrays in JavaScript, you may encounter situations where your array contains undefined values. This can happen when you're processing data from various sources or working with incomplete datasets. One common problem developers face is summing the values of an array with undefined elements. JavaScript’s reduce() method is a versatile tool that can help you solve this problem efficiently. In this article, we will explore how to use the reduce() method to sum an array, even when it includes undefined values, and ensure that the final result is correct. What is the reduce() Method in JavaScript? The ... Read More

Compute the sum of elements of an array which can be null or undefined JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:17:58

783 Views

Let’s say, we have an array of arrays, each containing some numbers along with some undefined and null values. We are required to create a new array that contains the sum of each corresponding sub array elements as its element. And the values undefined and null should be computed as 0.Following is the sample array −const arr = [[    12, 56, undefined, 5 ], [    undefined, 87, 2, null ], [    3, 6, 32, 1 ], [    undefined, null ]];The full code for this problem will be −Exampleconst arr = [[    12, 56, undefined, 5 ... Read More

Replace all characters in a string except the ones that exist in an array JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:16:29

378 Views

Let’s say, we have to write a function −replaceChar(str, arr, [char])Now, replace all characters of string str that are not present in array of strings arr with the optional argument char. If char is not provided, replace them with ‘*’.Let’s write the code for this function.The full code will be −Exampleconst arr = ['a', 'e', 'i', 'o', 'u']; const text = 'I looked for Mary and Samantha at the bus station.'; const replaceChar = (str, arr, char = '*') => {    const replacedString = str.split("").map(word => {       return arr.includes(word) ? word : char;    }).join("");   ... Read More

Sorting objects according to days name JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:15:03

2K+ Views

Let’s say, we have an array of objects that contains data about the humidity over the seven days of a week. The data, however, sits randomly in the array right now. We are supposed to sort the array of objects according to the days like data for Monday comes first, then Tuesday, Wednesday and lastly Sunday.Following is our array −const weather = [{    day: 'Wednesday',    humidity: 60 }, {    day: 'Saturday',    humidity: 50 }, {    day: 'Thursday',    humidity: 65 }, {    day: 'Monday',    humidity: 40 }, {    day: 'Sunday',    humidity: ... Read More

Advertisements