Found 6710 Articles for Javascript

Checking if a string contains all unique characters using JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:55:37

369 Views

ProblemWe are required to write a JavaScript function that takes in a sting and returns true if all the characters in the string appear only once and false otherwise.ExampleFollowing is the code − Live Democonst str = 'thisconaluqe'; const allUnique = (str = '') => {    for(let i = 0; i < str.length; i++){       const el = str[i];       if(str.indexOf(el) !== str.lastIndexOf(el)){          return false;       };    };    return true; }; console.log(allUnique(str));Outputtrue

Validating a boggle word using JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:55:19

499 Views

ProblemA Boggle board is a 2D array of individual characters, e.g. −const board = [    ["I", "L", "A", "W"],    ["B", "N", "G", "E"],    ["I", "U", "A", "O"],    ["A", "S", "R", "L"] ];We are required to write a JavaScript function that takes in boggle board and a string and checks whether that string is a valid guess in the boggle board or not. Valid guesses are strings which can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without reusing any previously used cells.For example, in the above board "LINGO", and "ILNBIA" would all be valid ... Read More

Product sum difference of digits of a number in JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:54:53

220 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should find the absolute difference between the sum and the product of all the digits of that number.ExampleFollowing is the code − Live Democonst num = 434312; const sumProductDifference = (num = 1) => {     const sum = String(num)         .split('')         .reduce((acc, val) => acc + +val, 0);       const product = String(num)         .split('')         .reduce((acc, val) => acc * +val, 1);       const diff = product - sum;       return Math.abs(diff); }; console.log(sumProductDifference(num));Output271

Removing all spaces from a string using JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:35:25

2K+ Views

In the vast realm of JavaScript programming, the ability to eliminate all spaces from a string assumes paramount importance. Efficiently removing spaces from a string is an essential skill that empowers developers to manipulate and process textual data with precision. By leveraging the inherent capabilities of JavaScript, programmers can employ various techniques to eradicate spaces and enhance the functionality and integrity of their applications. In this article, we will delve into the intricacies of eliminating spaces from a string using JavaScript, exploring lesser-known methods and algorithms that pave the way for streamlined text processing and manipulation. Problem Statement In the ... Read More

Returning the value of (count of positive / sum of negatives) for an array in JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:54:17

600 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers (positives and negatives) and our function should return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers.ExampleFollowing is the code − Live Democonst arr = [1, 2, 1, -2, -4, 2, -6, 2, -4, 9]; const posNeg = (arr = []) => {    const creds = arr.reduce((acc, val) => {       let [count, sum] = acc;       if(val > 0){          count++;       }else if(val < 0){          sum += val;       };       return [count, sum];    }, [0, 0]);    return creds; }; console.log(posNeg(arr));Output[ 6, -16 ]

Finding the nth palindrome number amongst whole numbers in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:37:23

527 Views

ProblemWe are required to write a JavaScript function that takes in number n. Our function should return the nth palindrome number if started counting from 0.For instance, the first palindrome will be 0, second will be 1, tenth will be 9, eleventh will be 11 as 10 is not a palindrome.ExampleFollowing is the code − Live Democonst num = 31; const findNthPalindrome = (num = 1) => {    const isPalindrome = (num = 1) => {       const reverse = +String(num)       .split('')       .reverse()       .join('');       return reverse === num;    };    let count = 0;    let i = 0;    while(count < num){       if(isPalindrome(i)){          count++;       };       i++;    };    return i - 1; }; console.log(findNthPalindrome(num));OutputFollowing is the console output −212

Smallest number formed by shuffling one digit at most in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:34:59

215 Views

ProblemWe are required to write a JavaScript function that takes in a positive number n. We can do at most one operation −Choosing the index of a digit in the number, remove this digit at that index and insert it back to another or at the same place in the number in order to find the smallest number we can get.Our function should return this smallest number.ExampleFollowing is the code − Live Democonst num = 354166; const smallestShuffle = (num) => {    const arr = String(num).split('');    const { ind } = arr.reduce((acc, val, index) => {       ... Read More

Finding life path number based on a date of birth in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:32:10

460 Views

Life Path NumberA person's Life Path Number is calculated by adding each individual number in that person's date of birth, until it is reduced to a single digit number.ProblemWe are required to write a JavaScript function that takes in a date in “yyyy-mm-dd” format and returns the life path number for that date of birth.For instance, if the date is: 1999-06-10year : 1 + 9 + 9 + 9 = 28 → 2 + 8 = 10 → 1 + 0 = 1 month : 0 + 6 = 6 day : 1 + 0 = 1 result: 1 + ... Read More

Longest possible string built from two strings in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:29:53

523 Views

ProblemWe are required to write a JavaScript function that takes in two strings s1 and s2 including only letters from ato z.Our function should return a new sorted string, the longest possible, containing distinct letters - each taken only once - coming from s1 or s2.ExampleFollowing is the code − Live Democonst str1 = "xyaabbbccccdefww"; const str2 = "xxxxyyyyabklmopq"; const longestPossible = (str1 = '', str2 = '') => {    const combined = str1.concat(str2);    const lower = combined.toLowerCase();    const split =lower.split('');    const sorted = split.sort();    const res = [];    for(const el of sorted){     ... Read More

Adding and searching for words in custom Data Structure in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:27:39

182 Views

ProblemWe are required to design a data structure in JavaScript that supports the following two operations −addWord, which adds a word to that Data Structure (DS), we can take help of existing DS like arrays or any other DS to store this data, search, which searches a literal word or a regular expression string containing lowercase letters "a-z" or "." where "." can represent any letterFor exampleaddWord("sir") addWord("car") addWord("mad") search("hell") === false search(".ad") === true search("s..") === trueExampleFollowing is the code − Live Democlass MyData{    constructor(){       this.arr = [];    }; }; MyData.prototype.addWord = function (word) { ... Read More

Advertisements