Found 6710 Articles for Javascript

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

The 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

Swapping letter with succeeding alphabet in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:24:48

204 Views

We are required to write a JavaScript function that takes in a string and changes every letter of the string from the English alphabets to its succeeding element.For example: If the string is −const str = 'how are you';OutputThen the output should be −const output = 'ipx bsf zpv'Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'how are you'; const isAlpha = code => (code >= 65 && code = 97 && code code === 90 || code === 122; const nextLetterString = str => {    const strArr = ... Read More

Taking the absolute sum of Array of Numbers in JavaScript

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

509 Views

We are required to write a JavaScript function that takes in an array with both positive and negative numbers and returns the absolute sum of all the elements of the array. We are required to do this without taking help of any inbuilt library function.For example: If the array is −const arr = [1, -5, -34, -5, 2, 5, 6];OutputThen the output should be −58Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [1, -5, -34, -5, 2, 5, 6]; const absoluteSum = arr => {    let res = 0;   ... Read More

Column sum of elements of 2-D arrays in JavaScript

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

1K+ Views

We have an array of arrays and are required to write a function that takes in this array and returns a new array that represents the sum of corresponding elements of original array.If the original array is −[    [43, 2, 21],    [1, 2, 4, 54],    [5, 84, 2],    [11, 5, 3, 1] ]OutputThen the output should be −[60, 93, 30, 55]Let’s write the function addArray() −ExampleThe full code for this function will be −const arr = [    [43, 2, 21],    [1, 2, 4, 54],    [5, 84, 2],    [11, 5, 3, 1] ]; ... Read More

Greatest digit of a number in JavaScript

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

566 Views

We are required to write a JavaScript recursive function that takes in a number and returns the greatest digit in the number.For example: If the number is 45654356Then the return value should be 6ExampleThe code for this will be −const num = 45654356; const greatestDigit = (num = 0, greatest = 0) => {    if(num){       const max = Math.max(num % 10, greatest);       return greatestDigit(Math.floor(num / 10), max);    };    return greatest; }; console.log(greatestDigit(num));OutputThe output in the console will be −6

Vowel gaps array in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:12:31

118 Views

We are required to write a JavaScript function that takes in a string with at least one vowel, and for each character in the string we have to map a number in a string representing its nearest distance from a vowel.For example: If the string is −const str = 'vatghvf';OutputThen the output should be −const output = [1, 0, 1, 2, 3, 4, 5];Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'vatghvf'; const nearest = (arr = [], el) => arr.reduce((acc, val) => Math.min(acc, Math.abs(val - el)), Infinity); const vowelNearestDistance = ... Read More

Changing the case of a string using JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:10:37

180 Views

We are required to write a JavaScript function that takes in a string and converts it to snake case.Snake case is basically a style of writing strings by replacing the spaces with '_' and converting the first letter of each word to lowercase.ExampleThe code for this will be −const str = 'This is a simple sentence'; const toSnakeCase = (str = '') => {    const strArr = str.split(' ');    const snakeArr = strArr.reduce((acc, val) => {       return acc.concat(val.toLowerCase());    }, []);    return snakeArr.join('_'); }; console.log(toSnakeCase(str));OutputThe output in the console will be −this_is_a_simple_sentenceRead More

Finding the index of the first repeating character in a string in JavaScript

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

579 Views

We are required to write a JavaScript function that takes in a string and returns the index of the first character that appears twice in the string. If there is no such character then we should return -1.Let’s say the following is our string −const str = 'Hello world, how are you';We need to find the index of the first repeating character.ExampleThe code for this will be −const str = 'Hello world, how are you'; const firstRepeating = str => {    const map = new Map();    for(let i = 0; i < str.length; i++){       if(map.has(str[i])){ ... Read More

Differences in two strings in JavaScript

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

448 Views

We are required to write a JavaScript function that takes in two strings and find the number of corresponding dissimilarities in the strings. The corresponding elements will be dissimilar if they are not equalExampleLet’s say the following are our strings −const str1 = 'Hello world!!!'; const str2 = 'Hellp world111';ExampleThe code for this will be −const str1 = 'Hello world!!!'; const str2 = 'Hellp world111'; const dissimilarity = (str1 = '', str2 = '') => {    let count = 0;    for(let i = 0; i < str1.length; i++){       if(str1[i] === str2[i]){         ... Read More

Advertisements