Found 6710 Articles for Javascript

All possible odd length subarrays JavaScript

Nikitasha Shrivastava
Updated on 18-May-2023 11:28:52

459 Views

In this problem statement, our task is to find all the possible odd length subarrays with the help of Javascript functionalities. This task can be done with the help of some built in functions of Javascript or we can solve it by multiple for loops. Logic for the given problem The problem stated that we have to get all the possible odd length of the subarrays in Javascript programming language. The meaning of add length is that the length of the subarrays should be 1, 3, 5, 7, .....end so on. So our task is to filter the length ... Read More

Special arrays in JavaScript

Nikitasha Shrivastava
Updated on 18-May-2023 16:02:38

506 Views

In this problem statement, our task is to show working of some special arrays and their characteristics in Javascript programming. In Javascript a special array can be dependent on the given context. The special arrays can refer to different types of arrays depending on the condition and context. There are some interpretations possible: Typed arrays, Sparse arrays and Array with string keys. What is the Typed Array? In Javascript there are many built-in methods available for typed array objects. These arrays are special in the context that they represent the array of a specific type of data in it. ... Read More

Average of array excluding min max JavaScript

Nikitasha Shrivastava
Updated on 18-May-2023 11:31:23

799 Views

In this problem statement, our task is to find the average of the array excluding minimum and maximum values from the array with the help of Javascript functionalities. This task can be done by excluding the minimum and maximum values and calculating the average of the rest of the elements. Logic for the given problem To create the program for this problem we will use the Math function of Javascript to get the minimum and maximum values with the help of min and max keywords. After we are having the values of min and max now we will ... Read More

Finding longest substring between two same characters JavaScript

Nikitasha Shrivastava
Updated on 18-May-2023 12:25:47

510 Views

In this problem statement, our task is to find the longest substring between two same characters with the help of Javascript functionalities. This task can be done with the help of map function to keep track of the two same characters. Logic for the given problem The problem stated that we have to find the longest substring between two same characters in the given string. For example if we have a string like 'abcdefa', there are two same characters 'a' that have 5 characters called 'bcdef', so this will be called longest substring. For implementing the above given problem, ... Read More

Sorting array of Number by increasing frequency JavaScript

AmitDiwan
Updated on 25-Nov-2020 05:14:30

1K+ Views

We are required to write a JavaScript function that takes in an array of numbers that might contain some repeating numbers.The function should sort the array such that the elements that are repeated for the least number of times appears first followed by the elements with increasing frequency.For example −If the input array is −const arr = [1, 1, 2, 2, 2, 3];Then the sorted array should be −const output = [3, 1, 1, 2, 2, 2];Exampleconst arr = [1, 1, 2, 2, 2, 3]; const frequencySort = (arr = []) => {    let map = {};    for ... Read More

Can form target array from source array JavaScript

AmitDiwan
Updated on 25-Nov-2020 05:13:24

187 Views

We are given an array of distinct integers, let’s say arr, and another array of integer arrays, let say sourceArr.In the sourceArr array, the integers are distinct. We should write a function that forms arr by concatenating the arrays in sourceArr in any order.However, we cannot reorder the integers inside of any subarray in the soureArr. We should return true if it is possible to form the array arr from sourceArr, false otherwise.For example −const arr = [23, 67, 789]; const sourceArr = [[23], [789, 67]];The function should return false because we cannot reorder the elements inside a subarray and ... Read More

Compare Strings in JavaScript and return percentage of likeliness

AmitDiwan
Updated on 25-Nov-2020 05:12:03

1K+ Views

We are required to write a JavaScript function that can compare two strings and return the percentage likeliness of how much they are alike. The percentage will be nothing but a measure of many characters the two strings have in common.If they are completely similar the output should be 100, and if they contain no common character at all, the output should be 0.Exampleconst calculateSimilarity = (str1 = '', str2 = '') => {    let longer = str1;    let shorter = str2;    if (str1.length < str2.length) {       longer = str2; shorter = str1;   ... Read More

Computing the Cartesian Product of Multiple Arrays in JavaScript

AmitDiwan
Updated on 31-Mar-2023 15:39:43

664 Views

We are required to write a JavaScript function that takes in multiple arrays of numbers. The function should return an array of the cartesian product of the elements from all the arrays.For example −If the input arrays are −[1, 2], [10, 20], [100, 200, 300]Then the output should be −const output = [    [ 1, 10, 100 ],    [ 1, 10, 200 ],    [ 1, 10, 300 ],    [ 1, 20, 100 ],    [ 1, 20, 200 ],    [ 1, 20, 300 ],    [ 2, 10, 100 ],    [ 2, 10, 200 ],    [ 2, 10, 300 ],    [ 2, 20, 100 ],    [ 2, 20, 200 ],    [ 2, 20, 300 ] ];Exampleconst arr1 = [1, 2]; const arr2 = [10, 20]; const arr3 = [100, 200, 300]; const cartesianProduct = (...arr) => {    return arr.reduce((acc,val) => {       return acc.map(el => {          return val.map(element => {             return el.concat([element]);          });       }).reduce((acc,val) => acc.concat(val) ,[]);    }, [[]]); }; console.log(cartesianProduct(arr1, arr2, arr3));OutputThis will produce the following output −[    [ 1, 10, 100 ], [ 1, 10, 200 ],    [ 1, 10, 300 ], [ 1, 20, 100 ],    [ 1, 20, 200 ], [ 1, 20, 300 ],    [ 2, 10, 100 ], [ 2, 10, 200 ],    [ 2, 10, 300 ], [ 2, 20, 100 ],    [ 2, 20, 200 ], [ 2, 20, 300 ] ]

Check if a string is entirely made of the same substring JavaScript

AmitDiwan
Updated on 25-Nov-2020 05:08:14

363 Views

We are required to write a JavaScript function that takes in a string. It should return true or false based on whether the input consists of a repeated character sequence.The length of the given string is always greater than 1 and the character sequence must have at least one repetition.For example −"aa" should return true because it entirely contains two strings "a""aaa" should return true because it entirely contains three string "a""abcabcabc" should return true because it entirely containas three strings "abc""aba" should return false because it at least there should be two same substrings and nothing more"ababa" should return ... Read More

Insert a number into a sorted array of numbers JavaScript

AmitDiwan
Updated on 25-Nov-2020 05:05:49

1K+ Views

We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a single number as the second argument.The function should push the number specified as the second argument into the array without distorting the sorting of the elements.We are required to do this without creating another array.Exampleconst arr = [6, 7, 8, 9, 12, 14, 16, 17, 19, 20, 22]; const num = 15; const findIndex = (arr, val) => {    let low = 0, high = arr.length;    while (low < high) {       let mid ... Read More

Advertisements