Change Every Letter to Next Letter in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:06:46

726 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';Then the output should be −const output = 'ipx bsf zpv'ExampleFollowing is the code −const str = 'how are you'; const isAlpha = code => (code >= 65 && code = 97 && code code === 90 || code === 122; const nextLetterString = str => {    const strArr = str.split('');    return strArr.reduce((acc, val) => {       ... Read More

Absolute Sum of Array Elements in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:05:34

798 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];Then the output should be −58ExampleFollowing is the code −const arr = [1, -5, -34, -5, 2, 5, 6]; const absoluteSum = arr => {    let res = 0;    for(let i = 0; i < arr.length; i++){       ... Read More

Convert String to Binary String in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:04:13

656 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.ExampleFollowing is the code −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 += s[i]; ... Read More

Count Numbers Divisible by M in a Given Range in C++

Sunidhi Bansal
Updated on 16-Sep-2020 10:03:55

2K+ Views

We are given three numbers A,B and M. A and B define the range [A,B] of numbers.The goal is to count numbers between A and B that are divisible by M.We will start from i=A till first multiple of M. Increment count if i%M=0. Now increment i till i

Find Greatest Digit by Recursion in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:02:46

315 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 6ExampleFollowing is the code −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));OutputFollowing is the output in the console −6

Calculating Resistance of N Devices in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:01:45

224 Views

In Physics, the equivalent resistance of say 3 resistors connected in series is given by −R = R1 + R2 + R3And the equivalent resistance of resistors connected in parallel is given by −R = (1/R1) + (1/R2) + (1/R3)We are required to write a JavaScript function that takes a string having two possible values, 'series' or 'parallel' and then n numbers representing the resistance of n resistors.And the function should return the equivalent resistance of these resistors.ExampleLet us write the code for this function.const r1 = 5, r2 = 7, r3 = 9; const equivalentResistance = (combination = 'parallel', ... Read More

Count Valid Pairs in Array Satisfying Given Conditions in C++

Sunidhi Bansal
Updated on 16-Sep-2020 10:00:45

602 Views

We are given with an array arr[] of N elements. The goal is to find the count of all valid pairs (Arr[i],Arr[j]) that follow certain conditions. Pairs Arr[i],Arr[j] invalid if −Arr[i]==Arr[j]Arr[i]+Arr[j] is eveni+j

Distance to Nearest Vowel in a String Using JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:00:07

462 Views

We are required to write a JavaScript function that takes in a string with atleast 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';Then the output should be −const output = [1, 0, 1, 2, 3, 4, 5];ExampleFollowing is the code −const str = 'vatghvf'; const nearest = (arr = [], el) => arr.reduce((acc, val) => Math.min(acc, Math.abs(val - el)), Infinity); const vowelNearestDistance = (str = '') => {    const s = str.toLowerCase();   ... Read More

Valid Triangle Edges in JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:58:25

470 Views

Suppose, we have three lines of length, respectively l, m and n. These three lines can only form a triangle, if the sum of any arbitrary two sides is greater than the third side.For example, if the length of three lines is 4, 9 and 3, they cannot form a triangle because 4+3 is less than 9.We are required to write a JavaScript function that the three numbers represents the length of three sides and checks whether they can form a triangle or not.ExampleFollowing is the code −const a = 9, b = 5, c = 3; const isValidTriangle = ... Read More

Casting a String to Snake Case in JavaScript

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

2K+ Views

Snake case is basically a style of writing strings by replacing the spaces with '_' and converting the first letter of each word to lowercase.We are required to write a JavaScript function that takes in a string and converts it to snake case.ExampleFollowing is the code −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));OutputFollowing is the output in the console −this_is_a_simple_sentence

Advertisements