Found 10483 Articles for Web Development

Reversing the prime length words - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:18:30

199 Views

We are required to write a JavaScript function that takes in a string that contains strings joined by whitespaces. Our function should create a new string that has all the words from the original string and the words whose length is a prime number reversed i.e. words with length 2, 3, 5, 7, 100, etc.ExampleFollowing is the code −const str = 'His father is an engineer by profession'; // helper functions const isPrime = n => {    if (n===1){       return false;    }else if(n === 2){       return true;    }else{       ... Read More

Temperature converter using JavaScript

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

579 Views

We are required to write a JavaScript function that takes in a string representing a temperature either in Celsius or in Fahrenheit.Like this −"23F", "43C", "23F"We are required to write a JavaScript function that takes in this string and converts the temperature from Celsius to Fahrenheit and Fahrenheit to Celsius.ExampleFollowing is the code −const temp1 = '37C'; const temp2 = '100F'; const tempConverter = temp => {    const degree = temp[temp.length-1];    let converted;    if(degree === "C") {       converted = (parseInt(temp) * 9 / 5 + 32).toFixed(2) + "F";    }else {       ... Read More

JavaScript - Checking for pandigital numbers

Alshifa Hasnain
Updated on 24-Dec-2024 17:46:09

528 Views

In this article, we will learn two approaches to check if a number is pandigital using JavaScript. Pandigital numbers are fascinating because they include each digit at least once within a specific range. Checking if a number is pandigital can be a common problem in programming challenges or number theory explorations.  What is a Pandigital Number? A Pandigital number is a number that includes all the digits within a specific range at least once. For example − A 0-9 pandigital number contains each digit from 0 to 9 at least once, regardless of order. ... Read More

Joining two strings with two words at a time - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:14:35

160 Views

We are required to write a JavaScript function that takes in two strings, creates and returns a new string with first two words of first string, next two words of second string, then first, then second and so on.For example −If the strings are −const str1 = 'Hello world'; const str2 = 'How are you btw';Then the output should be −const output = 'HeHollw o arwoe rlyodu btw';ExampleLet us write the code for this function −const str1 = 'Hello world'; const str2 = 'How are you btw'; const twiceJoin = (str1 = '', str2 = '') => {    let ... Read More

Returning the second most frequent character from a string (including spaces) - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:13:24

399 Views

We are required to write a JavaScript function that takes in a string and returns the character which makes second most appearances in the string.ExampleFollowing is the code −const str = 'Hello world, I have never seen such a beautiful weather in the world'; const secondFrequent = str => {    const map = {};    for(let i = 0; i < str.length; i++){       map[str[i]] = (map[str[i]] || 0) + 1;    };    const freqArr = Object.keys(map).map(el => [el, map[el]]);    freqArr.sort((a, b) => b[1] - a[1]);    return freqArr[1][0]; }; console.log(secondFrequent(str));OutputFollowing is the output in the console −e

Mapping unique characters of string to an array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:12:26

419 Views

We are required to write a JavaScript function that takes in a string and starts mapping its characters from 0. And every time the function encounters a unique (non-duplicate) character, it should increase the mapping count by 1 otherwise map the same number for duplicate characters.For example − If the string is −const str = 'heeeyyyy';Then the output should be −const output = [0, 1, 1, 1, 2, 2, 2, 2];ExampleFollowing is the code −const str = 'heeeyyyy'; const mapString = str => {    const res = [];    let curr = '', count = -1;    for(let i ... Read More

Checking semiprime numbers - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:11:12

391 Views

We are required to write a JavaScript function that takes in a number and the function establishes if the provided number is a semiprime or not.SemiprimeA semiprime number is that number which is a special type of composite number that is a product of two prime numbers. For example: 6, 15, 10, 77 are all semiprime. The square of a prime number is also semiprime, like 4, 9, 25 etc.ExampleFollowing is the code to check semi-prime numbers −const num = 141; const checkSemiprime = num => {    let cnt = 0;    for (let i = 2; cnt < ... Read More

Sorting digits of all the number of array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:10:00

783 Views

We are required to write a JavaScript function that takes in an array of numbers and reorders the digit of all the numbers internally in a specific order (lets say in ascending order for the sake of this problem).For example − If the array is −const arr = [543, 65, 343, 75, 567, 878, 87];Then the output should be −const output = [345, 56, 334, 57, 567, 788, 78];ExampleFollowing is the code −const arr = [543, 65, 343, 75, 567, 878, 87]; const ascendNumber = num => {    const numArr = String(num).split('').map(el => +el);    numArr.sort((a, b) => a ... Read More

Counting the number of redundant characters in a string - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:08:49

278 Views

We are required to write a JavaScript function that takes in a string and returns the count of redundant characters in the string.For example − If the string is −const str = 'abcde'Then the output should be 0If the string is −const str = 'aaacbfsc';Then the output should be 3ExampleFollowing is the code −const str = 'aaacbfsc'; const countRedundant = str => {    let count = 0;    for(let i = 0; i < str.length; i++){       if(i === str.lastIndexOf(str[i])){          continue;       };       count++;    };    return count; }; console.log(countRedundant(str));OutputFollowing is the output in the console −3

Checking progressive array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:07:43

205 Views

We are required to write a JavaScript function that takes in an array of strings, ordered by ascending length.The function should return true if, for each pair of consecutive strings, the second string can be formed from the first by adding a single letter either at the beginning or end.For example: If the array is given by −const arr = ["c", "ca", "can", "acan", "acane", "dacane"];Then our function should return true.ExampleFollowing is the code −const arr = ["c", "ca", "can", "acan", "acane", "dacane"]; const isProgressive = arr => {    for(let i = 0; i < arr.length-1; i++){     ... Read More

Advertisements