Found 6710 Articles for Javascript

Return the sum of two consecutive elements from the original array in JavaScript

Nikhilesh Aleti
Updated on 23-Sep-2022 11:09:39

2K+ Views

An Array is known as collection of similar data elements which are stored at contiguous memory locations. Array is such a simple data structure where we can access each data element in the array with the help index numbers, which starts from 0. The following is the basic declaration of array in JavaScript − const mobiles = ["SAMSUNG", "ONEPLUS", "APPLE"]; Sum of two consecutive elements of an array We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of two consecutive elements from ... Read More

Finding pandigital numbers using JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:49:16

187 Views

We are required to write a JavaScript function that takes in a string representing a number. The function returns true if the number is pandigital, false otherwise.A pandigital number is a number that contains all digits (0-9) at least once.Therefore, let’s write the code for this function −ExampleThe code for this will be −const numStr1 = '47458892414'; const numStr2 = '53657687691428890'; const isPandigital = numStr => {    let legend = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];    for(let i = 0; i < numStr.length; i++){       if(!legend.includes(numStr[i])){          continue;   ... Read More

Twice join of two strings in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:47:42

149 Views

We are required to write a JavaScript function that takes in two strings; creates and returns a new string with first 2 words of first string, next two words of second string, then first, then second and so on.For example: If the strings are −const str1 = 'Hello world'; const str2 = 'How are you btw';Then the output should be −const output = 'HeHollw o arwoe rlyodu btw';Therefore, let’s write the code for this function −ExampleThe code for this will be −const str1 = 'Hello world'; const str2 = 'How are you btw'; const twiceJoin = (str1 = '', str2 ... Read More

Finding the second most frequent character in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:45:37

283 Views

We are required to write a JavaScript function that takes in a string and returns the character which makes second most appearances in the string.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'Hello world, I have never seen such a beautiful weather in the world'; const secondFrequent = str => {    const map = {};    for(let i = 0; i < str.length; i++){       map[str[i]] = (map[str[i]] || 0) + 1;    };    const freqArr = Object.keys(map).map(el => [el, map[el]]);    freqArr.sort((a, b) => b[1] - ... Read More

Constructing array from string unique characters in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:43:20

145 Views

We are required to write a JavaScript function that takes in a string and starts mapping its characters from 0.And every time, the function encounters a unique (non-duplicate) character it should increase the mapping count by 1 otherwise it should map the same number for duplicate characters.For example: If the string is −const str = 'heeeyyyy';Then the output should be −const output = [0, 1, 1, 1, 2, 2, 2, 2];Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'heeeyyyy'; const mapString = str => {    const res = [];   ... Read More

Sorting the numbers from within in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:41:09

150 Views

We are required to write a JavaScript function that takes in an array of numbers and reorders the digit of all the numbers internally in a specific order (let’s say in ascending order for the sake of this problem).For example: If the array is −const arr = [543, 65, 343, 75, 567, 878, 87];Then the output should be −const output = [345, 56, 334, 57, 567, 788, 78];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [543, 65, 343, 75, 567, 878, 87]; const ascendNumber = num => {    const numArr ... Read More

Number of non-unique characters in a string in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:39:20

222 Views

We are required to write a JavaScript function that takes in a string and returns the count of redundant characters in the string.For example: If the string is −const str = 'abcde'Then the output should be 0.If the string is −const str = 'aaacbfsc';Then the output should be 3.ExampleThe code for this will be −const str = 'aaacbfsc'; const countRedundant = str => {    let count = 0;    for(let i = 0; i < str.length; i++){       if(i === str.lastIndexOf(str[i])){          continue;       };       count++;    };    return count; }; console.log(countRedundant(str));OutputThe output in the console will be −3

Dynamic programming to check dynamic behavior of an array in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:37:29

533 Views

We are required to write a JavaScript function that takes in an array of strings, ordered by ascending length.The function should return true if, for each pair of consecutive strings, the second string can be formed from the first by adding a single letter either at the beginning or end.For example: If the array is given by −const arr = ["c", "ca", "can", "acan", "acane", "dacane"];Then our function should return trueTherefore, let’s write the code for this function.ExampleThe code for this will be −const arr = ["c", "ca", "can", "acan", "acane", "dacane"]; const isProgressive = arr => {    for(let ... Read More

Deleting the last vowel from a string in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:33:36

245 Views

We are required to write a JavaScript function that takes in a string and returns a new string with the last vowel of each word removed.For example: If the string is −const str = 'This is an example string';Then the output should be −const output = 'Ths s n exampl strng';Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'This is an example string'; const removeLast = word => {    const lastIndex = el => word.lastIndexOf(el);    const ind = Math.max(lastIndex('a'), lastIndex('e'), lastIndex('i'), lastIndex('o'), lastIndex('u'));    return word.substr(0, ind) + word.substr(ind+1, ... Read More

Performing the subtraction operation without the subtraction operator in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:31:45

141 Views

We are required to write a JavaScript function that takes in two numbers and returns their difference but without using the (-) sign.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num1 = 56; const num = 78; const subtractWithoutMinus = (num1, num2) => {    if(num2 === 0){       return num1;    };    return subtractWithoutMinus(num1 ^ num2, (~num1 & num2)

Advertisements