Found 8591 Articles for Front End Technology

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

223 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)

Picking the odd one out in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:30:24

541 Views

We are required to write a JavaScript function that takes in an array of literals that contains all similar elements but one.Our function should return the unlike number.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]; // considering that the length of array is atleast 3 const findUnlike = arr => {    for(let i = 1; i < arr.length-1; i++){       if(arr[i] - arr[i-1] !== 0 && arr[i]-arr[i+1] === 0){          return arr[i-1];     ... Read More

Interchanging a string to a binary string in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:25:17

181 Views

We are required to write a JavaScript function that takes in a lowercase string and returns a new string in which all the elements between [a, m] are represented by 0 and all the elements between [n, z] are represented by 1.ExampleThe code for this will be −const str = 'Hello worlld how are you'; const stringToBinary = (str = '') => {    const s = str.toLowerCase();    let res = '';    for(let i = 0; i < s.length; i++){       // for special characters       if(s[i].toLowerCase() === s[i].toUpperCase()){          res ... Read More

Advertisements