Found 6710 Articles for Javascript

Calculating resistance of n devices - JavaScript

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

223 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

Distance to nearest vowel in a string - 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 - JavaScript

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

466 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 - 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

Return index of first repeating character in a string - 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 the dissimilarities in two strings - JavaScript

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

142 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 - 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 word starting with particular characters - 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 - 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 - 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

Advertisements