Javascript Articles

Page 394 of 534

Odd even sort in an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 16-Sep-2020 1K+ Views

We are required to write a JavaScript function that takes in an array of numbers and sorts the array such that first all the even numbers appear in ascending order and then all the odd numbers appear in ascending order.For example: If the input array is −const arr = [2, 5, 2, 6, 7, 1, 8, 9];Then the output should be −const output = [2, 2, 6, 8, 1, 5, 7, 9];ExampleFollowing is the code −const arr = [2, 5, 2, 6, 7, 1, 8, 9]; const isEven = num => num % 2 === 0; const sorter = ((a, ...

Read More

Finding missing letter in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 16-Sep-2020 741 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 lettersWe are required to write a function that takes in one such string and returns the missing element from the stringExampleFollowing is the code −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))){          continue;       };     ...

Read More

Repeating letter string - JavaScript

AmitDiwan
AmitDiwan
Updated on 16-Sep-2020 1K+ 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 2Then the output should be −const output = 'hhooww  aarree  yyoouu'ExampleFollowing is the code −const str = 'how are you'; const repeatNTimes = (str, n) => {    let res = '';    for(let i = 0; i < str.length; i++){       // using ...

Read More

Change every letter to next letter - JavaScript

AmitDiwan
AmitDiwan
Updated on 16-Sep-2020 780 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 - JavaScript

AmitDiwan
AmitDiwan
Updated on 16-Sep-2020 866 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

Finding greatest digit by recursion - JavaScript

AmitDiwan
AmitDiwan
Updated on 16-Sep-2020 346 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

Read More

Calculating resistance of n devices - JavaScript

AmitDiwan
AmitDiwan
Updated on 16-Sep-2020 271 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
AmitDiwan
Updated on 16-Sep-2020 501 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
AmitDiwan
Updated on 16-Sep-2020 509 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
AmitDiwan
Updated on 16-Sep-2020 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

Read More
Showing 3931–3940 of 5,338 articles
« Prev 1 392 393 394 395 396 534 Next »
Advertisements