Reverse String While Maintaining Space Positions in JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:25:01

805 Views

ProblemWe are required to write a JavaScript function that takes in a string that might contain some spaces.Our function should reverse the words present in the string internally without interchange the characters of two separate words or the spaces.ExampleFollowing is the code − Live Democonst str = 'this is normal string'; const reverseWordsWithin = (str = '') => {    let res = "";    for (let i = str.length - 1; i >= 0; i--){       if(str[i] != " "){          res += str[i];       };       if(str[res.length] == " "){          res += str[res.length];       };    };    return res; }; console.log(reverseWordsWithin(str));Outputgnir ts lamron sisiht

Counting Prime Numbers that Reduce to 1 within a Range Using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:22:43

176 Views

ProblemWe are required to write a JavaScript function that takes in a range array of two numbers. Our function should return the count of such prime numbers whose squared sum of digits eventually yields 1.For instance, 23 is a prime number and,22 + 32 = 13 12 + 32 = 10 12 + 02 = 1Hence, 23 should be a valid number.ExampleFollowing is the code − Live Democonst range = [2, 212]; String.prototype.reduce = Array.prototype.reduce; const isPrime = (n) => {    if ( n acc+v*v, 0 )));    return n===1; } const countDesiredPrimes = ([a, b]) => {    let res=0;    for ( ; a

Swap Adjacent Binary Bits of a Decimal Using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:21:40

184 Views

ProblemWe are required to write a JavaScript function that takes in a number.Our function should iterate through the binary equivalent of the number and swap its adjacent bits to construct a new binary. And then finally our function should return the decimal equivalent of the new binary.ExampleFollowing is the code − Live Democonst num = 13; const swapBits = (num) => {    let arr = num.toString(2).split('');    if(arr.length % 2){       arr.unshift(0);    }    for(let i = 0; i < arr.length - 1; i = i + 2) {       [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];    }    return +('0b' + arr.join('')); } console.log(swapBits(num));Output14

Find Length of Longest Vowel Substring in a String Using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:21:18

541 Views

ProblemWe are required to write a JavaScript function that takes in a string. Our function should return the length of the longest contiguous substring that contains only vowels.ExampleFollowing is the code − Live Democonst str = 'schooeal'; const findLongestVowel = (str = '') => {    let cur = 0    let max = 0    for (let i = 0; i < str.length; ++i) {       if ("aeiou".includes(str[i])) {          cur++          if (cur > max) {             max = cur          }       } else {          cur = 0       }    }    return max }; console.log(findLongestVowel(str));Output4

Finding Alphabet from ASCII Value in JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:21:01

498 Views

ProblemWe are required to write a JavaScript function that takes in a number. Our function should return the corresponding ASCII alphabet for that number (if there exists an alphabet for that ASCII value), -1 otherwise.The condition here is that we cannot use any inbuilt function that converts these values.ExampleFollowing is the code − Live Democonst num = 98; const findChar = (num = 1) => {    const alpha = 'abcdefghijklmnopqrstuvwxyz';    if(num >= 97 && num = 65 && num

Apply Custom Function to Corresponding Elements of Two Arrays in JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:20:40

195 Views

ProblemWe are required to write a JavaScript function that takes in a callback function (which takes in two arguments and returns a value) as the first argument and two arrays of essentially the same length as the second and third argument.Our function should construct and return a new array whose each corresponding element is the return value of the callback function if corresponding numbers of the input array are provided to it.ExampleFollowing is the code − Live Democonst arr1 = [1, 2, 3, 4]; const arr2 = [5, 6, 7, 8]; const add = (a, b) => a + b; const ... Read More

Count Upside Down Numbers in a Range Using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:18:06

252 Views

Upside Down NumbersThe numbers that remain the same when they’re rotated by 180 degrees are called upside down numbers.For instance, 9116, 69.ProblemWe are required to write a JavaScript function that takes in a range array of two numbers. Our function should return the count of all the upside down numbers that fall in the specified range.ExampleFollowing is the code − Live Democonst range = [5, 125]; const flipNum = (number) => {    const upsideDownDigits = [0, 1, -99, -99, -99, -99, 9, -99, 8, 6];    let reverseNumArr = String(number)       .split('')       .map(val => Number(val)) ... Read More

Find Longest Consecutive Character Appearance in a String using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:17:43

247 Views

ProblemWe are required to write a JavaScript function that takes in a string as the first argument and a single character as the second argument.Our function should count and return the longest consecutive appearance of the the character in the string.ExampleFollowing is the code − Live Democonst str = 'abcdaaadse'; const char = 'a'; const countChars = (str = '', char = '') => {    const arr = str.split('');    let c = 0, max = 0;    for (let i = 0; i max){             max = c;          };       }else{          if(c > max){             max = c;          };          c = 0;       };    };    return max; }; console.log(countChars(str, char));Output3

Count of Pairs in an Array with Consecutive Numbers Using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:17:20

460 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers. Our function should return the count of such contagious pairs from the array that have consecutive numbers in them.ExampleFollowing is the code − Live Democonst arr = [1, 2, 5, 8, -4, -3, 7, 6, 5]; const countPairs = (arr = []) => {    let count = 0;    for (var i=0; i

All Ways to Divide Array of Strings into Parts in JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:17:01

292 Views

ProblemWe are required to write a JavaScript function that takes in an array of literals of at least two elements.Our function should return all the ways to divide the array into two non-empty parts.For instance −For the array ["az", "toto", "picaro", "zone", "kiwi"]The possibilities are −"(az, toto picaro zone kiwi)(az toto, picaro zone kiwi)(az toto picaro, zone kiwi)(az toto picaro zone, kiwi)"ExampleFollowing is the code − Live Democonst arr = ["az", "toto", "picaro", "zone", "kiwi"]; const findAllPossiblities = (arr = []) => {    let array;    const res = [];    for(let i = 1; i < arr.length; i++){   ... Read More

Advertisements