Return Index of First Repeating Character in a String using JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:56:20

218 Views

We are required to write a JavaScript function that takes in a string and returns the index of first character that appears twice in the string.If there is no such character then we should return -1. Following is our string −const str = 'Hello world, how are you';ExampleFollowing is the code −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])){          return map.get(str[i]);       };       map.set(str[i], i);   ... Read More

Find Dissimilarities in Two Strings using JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:54:57

143 Views

We are required to write a JavaScript function that takes in two strings and find the number of corresponding dissimilarities in the stringsThe corresponding elements will be dissimilar if they are not equal. Let’s say the following are our two string −const str1 = 'Hello world!!!'; const str2 = 'Hellp world111';ExampleFollowing is the code −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]){          continue; ... Read More

Reverse Only the Odd Length Words in JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:53:47

522 Views

We are required to write a JavaScript function that takes in a string and reverses the words in the string that have an odd number of characters in them.Any substring in the string qualifies to be a word, if either it is encapsulated by two spaces on either ends or present at the end or beginning and followed or preceded by a space.Let’s say the following is our string −const str = 'hello beautiful people';The odd length words are −hello beautifulExampleLet us write the code for this function.const str = 'hello beautiful people'; const idOdd = str => str.length % ... Read More

Reverse Words Starting with Particular Characters in JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:52:22

230 Views

We are required to write a JavaScript function that takes in a sentence string and a character and the function should reverse all the words in the string starting with that particular character.For example: If the string is −const str = 'hello world, how are you';Starting with a particular character ‘h’ −Then the output string should be −const output = 'olleh world, woh are you';That means, we have reversed the words starting with “h” i.e. Hello and How.ExampleFollowing is the code −const str = 'hello world, how are you'; const reverseStartingWith = (str, char) => {    const strArr = ... Read More

Neutralisation of Strings in JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:51:07

246 Views

We are required to write a JavaScript function that takes in a string that contains only '+' or '-' and we have to return either '+' or '-' based on the whole neutralisation result of the string.Like '++' results to '+' and '--' also results to '+' while '-+' or '+-' results to '-'.Following is our string −const str = '+++-+-++---+-+--+-';ExampleFollowing is the code −const str = '+++-+-++---+-+--+-'; const netResult = (str = '') => {    const strArr = str.split('');    return strArr.reduce((acc, val) => {       if(acc === val){          return '+';   ... Read More

Checking Oddish and Evenish Numbers in JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:49:52

254 Views

A number is Oddish if the sum of all of its digits is odd, and a number is Evenish if the sum of all of its digits is even.We are required to write a function that determines whether a number is Oddish or Evenish. We should return true of Oddish values and false for evenishExampleFollowing is the code −const num = 434667; const isOddish = (num, sum = 0) => {    if(num){       return isOddish(Math.floor(num / 10), sum + (num % 10));    };    return sum % 2 === 1; }; console.log(isOddish(num));OutputFollowing is the output in the console −false

Product of Two Numbers Using HOC in JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:48:45

272 Views

HOCHOC or Higher Order Functions in JavaScript are a special type of functions that receives another function as argument or have a function set as their return value or do both. HOC along with closures is a very powerful tool in JavaScript.We are required to write a JavaScript Higher Order Function that can be used to obtain the product of two numbers.ExampleFollowing is the code −const num1 = 24; const num2 = 5; const productHOC = num1 => {    return product = num2 => {       return num1 * num2;    }; }; console.log(productHOC(num1)(num2));OutputFollowing is the output in the console −120

Remove Zeros from Start and End in JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:38:09

144 Views

We are required to write a JavaScript function that takes in a number as a string and returns a new number string with all the leading and trailing 0s removedFor example: If the input is −const strNum = '054954000'Then the output should be −const output = '54954'ExampleFollowing is the code −const strNum = '054954000'; const removeZero = (str = '') => {    const res = '';    let startLen = 0, endLen = str.length-1;    while(str[startLen] === '0'){       startLen++;    };    while(str[endLen] === '0'){       endLen--;    };    return str.substring(startLen, endLen+1); }; console.log(removeZero(strNum));OutputFollowing is the output in the console −54954

Finding Special Array in JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:36:10

542 Views

An array is a special array if −--All the elements at odd indices are odd. --All the elements at even indices are even.We are required to write a JavaScript function that takes in an array and checks if its a special array or not.ExampleFollowing is the code −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const isSpecial = (arr = []) => {    for(let i = 0; i < arr.length; i++){       if(arr[i] % 2 === i % 2){          continue;       };       return false;    };    return true; }; console.log(isSpecial(arr));OutputFollowing is the output in the console −true

Move String Capital Letters to Front in JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:35:03

314 Views

We are required to write a JavaScript function that takes in a string with uppercase and lowercase letters. The function should return a string with all the uppercase letters moved to front of the string.For example: If the input string is −const str = 'heLLO woRlD';Then the output should be −const output = 'LLORDhe wol';ExampleFollowing is the code −const str = 'heLLO woRlD'; const moveCapitalToFront = (str = '') => {    let capitalIndex = 0;    const newStrArr = [];    for(let i = 0; i < str.length; i++){       if(str[i] !== str[i].toLowerCase()){         ... Read More

Advertisements