Found 6683 Articles for Javascript

Finding the only unique string in an array using JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:33:28

511 Views

Unraveling the enigma of finding the solitary unique string within an array using JavaScript holds paramount importance for developers seeking to optimize their code. With the ability to handle complex data structures, JavaScript empowers programmers to solve intricate problems efficiently. In this article, we delve into the intricacies of identifying the lone unique string amidst an array, employing an arsenal of rarely used but indispensable techniques. By mastering the step-by-step approach presented herein, developers will acquire the prowess to sift through arrays, discerning the one string that stands distinct from the rest. Brace yourself for a journey into the depths ... Read More

Summing numbers present in a string separated by spaces using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:11:08

428 Views

ProblemWe are required to write a JavaScript function that takes in a string which has integers inside it separated by spaces.The task of our function is to convert each integer in the string into an integer and return their sum.ExampleFollowing is the code − Live Democonst str = '1 5 12 76 2'; const sumStringNumbers = (str = '') => {    const findSum = (arr = []) => {       const sum = arr.reduce((acc, val) => acc + val);       return sum;    };    let sum = 0;    const arr = str       .split(' ')       .map(Number);    sum = findSum(arr);    return sum; }; console.log(sumStringNumbers(str));Output96

Alternating sum of elements of a two-dimensional array using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:10:47

212 Views

ProblemWe are required to write a JavaScript function that takes in a 2-Dimensional array of m X n order of numbers containing the same number of rows and columns.For this array, our function should count and return the following sum−$\sum_{i=1}^m \sum_{j=1}^n (-1)^{i+j}a_{ij}$ExampleFollowing is the code − Live Democonst arr = [    [4, 6, 3],    [1, 8, 7],    [2, 5, 9] ]; const alternateSum = (arr = []) => {    let sum = 0;    for(let i = 0; i < arr.length; i++){       for(let j = 0; j < arr[i].length; j++){          const multiplier = (i + j) % 2 === 0 ? 1 : -1;          sum += (multiplier * arr[i][j]);       };    };    return sum; }; console.log(alternateSum(arr));Output7

Returning the first number that equals its index in an array using JavaScript

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

83 Views

ProblemWe are required to write a JavaScript function that takes in an array of number. Our function should return that first number from the array whose value and 0-based index are the same given that there exists at least one such number in the array.ExampleFollowing is the code − Live Democonst arr = [9, 2, 1, 3, 6, 5]; const findFirstSimilar = (arr = []) => {    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(el === i){          return i;       };    }; }; console.log(findFirstSimilar(arr));Output3

Encrypting a string based on an algorithm using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:08:03

620 Views

ProblemWe are required to write a JavaScript function that takes in a string and encrypts it based on the following algorithm −The string contains only space separated words.We need to encrypt each word in the string using the following rules−The first letter needs to be converted to its ASCII code.The second letter needs to be switched with the last letter.Therefore, according to this, the string ‘good’ will be encrypted as ‘103doo’.ExampleFollowing is the code − Live Democonst str = 'good'; const encyptString = (str = '') => {    const [first, second] = str.split('');    const last = str[str.length - 1]; ... Read More

Encrypting a string based on an algorithm in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:25:34

171 Views

ProblemWe are required to write a JavaScript function that takes in a string and encrypts it based on the following algorithm −The string contains only space separated words.We need to encrypt each word in the string using the following rules −The first letter needs to be converted to its ASCII code.The second letter needs to be switched with the last letter.Therefore, according to this, the string ‘good’ will be encrypted as ‘103doo’.ExampleFollowing is the code − Live Democonst str = 'good'; const encyptString = (str = '') => {    const [first, second] = str.split('');    const last = str[str.length - ... Read More

Finding the first non-consecutive number in an array in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:23:41

296 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should return that first element from the array which is not the natural successor of its previous element.It means we should return that element which is not +1 its previous element given that there exists at least one such element in the array.ExampleFollowing is the code − Live Democonst arr = [1, 2, 3, 4, 6, 7, 8]; const findFirstNonConsecutive = (arr = []) => {    for(let i = 0; i < arr.length - 1; i++){       const el = arr[i]; ... Read More

Checking if decimals share at least two common 1 bits in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:21:38

67 Views

ProblemWe are required to write a JavaScript function that takes in two numbers. Our function should return true if the numbers have 1 in the binary representation at the same index twice, false otherwise.ExampleFollowing is the code − Live Democonst num1 = 10; const num2 = 15; const checkBits = (num1 = 1, num2 = 1) => {    let c = num1.toString(2).split('');    let d = num2.toString(2).split('');    if(c.length > d.length){       c = c.slice(c.length - d.length);    }else{       d = d.slice(d.length - c.length);    };    let count = 0;    for(let i = ... Read More

Limiting string up to a specified length in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:18:30

873 Views

ProblemWe are required to write a JavaScript function that takes in a string and a number. Our function should return the truncated version of the given string up to the given limit followed by "..." if the result is shorter than the original string otherwise our function should return the same string if nothing was truncated.ExampleFollowing is the code − Live Democonst str = 'Testing String'; const num = 8; const limitString = (str = '', num = 1) => {    const { length: len } = str;    if(num < len){       return str.slice(0, num) + '...';    }else{       return str;    }; }; console.log(limitString(str, num));OutputFollowing is the console output −Testing ...

Constructing a string based on character matrix and number array in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:16:16

139 Views

ProblemWe are required to write a JavaScript function that takes in an n * n matrix of string characters and an array of integers (positive and unique).Our function should construct a string of those characters whose 1-based index is present in the array of numbers.Character Matrix −[    [‘a’, ‘b’, ‘c’, d’],    [‘o’, ‘f’, ‘r’, ‘g’],    [‘h’, ‘i’, ‘e’, ‘j’],    [‘k’, ‘l’, ‘m’, n’] ];Number Array −[1, 4, 5, 7, 11]Should return ‘adore’ because these are the characters present at the 1-based indices specified by number array in the matrix.ExampleFollowing is the code − Live Democonst arr = ... Read More

Advertisements