Check Array Elements for Sequence Formation in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:46:11

142 Views

We are required to write a JavaScript function that takes in an array of numbers and checks if the elements of the array can be rearranged to form a sequence of numbers or not.For example: If the array is −const arr = [3, 1, 4, 2, 5];Then the output should be true.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [3, 1, 4, 2, 5]; const canBeConsecutive = (arr = []) => {    if(!arr.length){       return false;    };    const copy = arr.slice();    copy.sort((a, b) => a ... Read More

Finding Special Kind of Sentences Smooth in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:44:04

241 Views

We are required to write a JavaScript function that checks whether a sentence is smooth or not.A sentence is smooth when the first letter of each word in the sentence is the same as the last letter of its preceding word.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'this stringt tries sto obe esmooth'; const str2 = 'this string is not smooth'; const isSmooth = str => {    const strArr = str.split(' ');    for(let i = 0; i < strArr.length; i++){       if(!strArr[i+1] || strArr[i][strArr[i].length -1] ===strArr[i+1][0]){ ... Read More

Finding the Inclination of Arrays in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:40:33

126 Views

We are required to write a JavaScript function that takes in an array of numbers and returns true if it’s either strictly increasing or strictly decreasing, otherwise returns false.In Mathematics, a strictly increasing function is that function in which the value to be plotted always increases. Similarly, a strictly decreasing function is that function in which the value to be plotted always decreases.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [12, 45, 6, 4, 23, 23, 21, 1]; const arr2 = [12, 45, 67, 89, 123, 144, 2656, 5657]; const sameSlope ... Read More

Finding Smallest Number That Satisfies Conditions in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:38:42

219 Views

We are required to write a JavaScript function that takes in a number as the first argument, say n, and an array of numbers as the second argument. The function should return the smallest n digit number which is a multiple of all the elements specified in the array.If there exist no such n digit element then we should return the smallest such element.For example: If the array is −const arr = [12, 4, 5, 10, 9]For both n = 2 and n = 3, OutputThe output should be −180Therefore, let’s write the code for this function −ExampleThe code for ... Read More

Find Simplest Form of Summed Fractions in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:36:56

111 Views

We have an array of arrays like this −const arr = [[12, 56], [3, 45], [23, 2], [2, 6], [2, 8]];Note that while the array can have any number of elements, each subarray should strictly contain two numbers.The two numbers in each subarray represents a fraction. Like the fraction represented by the first subarray is 12/56, by the second is 3/45 and so on.We are required to write a JavaScript function that takes in one such array and calculates the sum of fractions represented by all the subarrays.We are required to calculate the sum in fraction form (i.e., without converting ... Read More

Absolute Difference of Corresponding Digits in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:34:57

182 Views

We are required to write a JavaScript function that takes in two numbers a and b returns their digit distance.Digit distance:The digit distance of two numbers is the absolute sum of the difference between their corresponding digits.For example:If the numbers are: 345 and 678Then the digit distance will be −|3-6| + |4-7| + |5-8| = 3 + 3 + 3 = 9Therefore, let’s write the code for this function −ExampleThe code for this will be −const num1 = 345; const num2 = 678; const digitDistance = (a, b) => {    const aStr = String(a);    const bStr = String(b); ... Read More

Detect First Non-Repeating String in Array in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:32:53

1K+ Views

Suppose, we have an array of strings like this where strings might contain duplicate characters −const arr = ['54gdgdfe3', '434ffd', '43frdf', '43fdhnh', 'wgcxhjny', 'fsdf34'];We are required to write a JavaScript function that takes in one such array and returns the very first element from the array that contains 0 duplicate characters. If there does not exist any such string, we should return false.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = ['54gdgdfe3', '434ffd', '43frdf', '43fdhnh', 'wgcxhjny', 'fsdf34']; const isUnique = str => {    return str.split('').every(el => str.indexOf(el) === str.lastIndexOf(el)); }; const ... Read More

Special Type of Sort of Array of Numbers in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:29:52

130 Views

We are required to write a JavaScript function that takes in an array of numbers and sorts the array such that first all the even numbers appear in ascending order and then all the odd numbers appear in ascending order.For example: If the input array is −const arr = [2, 5, 2, 6, 7, 1, 8, 9];OutputThen the output should be −const output = [2, 2, 6, 8, 1, 5, 7, 9];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [2, 5, 2, 6, 7, 1, 8, 9]; const isEven = num ... Read More

Find Missing Letter from Strings in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:28:21

196 Views

We have a string of length m that contains first m letters of the English alphabets, but somehow, one element went missing from the string. So now the string contains m-1 letters.We are required to write a function that takes in one such string and returns the missing element from the stringTherefore, let’s write the code for this function −ExampleThe code for this will be −const str = "acdghfbekj"; const missingCharacter = str => {    // to make the function more consistent    const s = str.toLowerCase();    for(let i = 97; ; i++){       if(s.includes(String.fromCharCode(i))){   ... Read More

N Times Dribbling Strings in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:26:15

100 Views

We are required to write a JavaScript function that takes in a string and a number, say n, and the function should return a new string in which all the letters of the original string are repeated n times.For example: If the string is −const str = 'how are you'And the number n is 2.OutputThen the output should be −const output = 'hhooww aarree yyoouu'Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'how are you'; const repeatNTimes = (str, n) => {    let res = '';    for(let i = ... Read More

Advertisements