Javascript Articles

Page 34 of 534

Constructing a sentence based on array of words and punctuations using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 863 Views

ProblemWe are required to write a JavaScript function that takes in an array of words and punctuations. Our function should join array elements to construct a sentence based on the following rules −there must always be a space between words;there must not be a space between a comma and word on the left;there must always be one and only one period at the end of a sentence.ExampleFollowing is the code −const arr = ['hey', ', ', 'and', ', ', 'you']; const buildSentence = (arr = []) => {    let res = '';    for(let i = 0; i < ...

Read More

Finding the largest 5 digit number within the input number using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 491 Views

ProblemWe are required to write a JavaScript function that takes in a string number of at least five digits. Our function should return the greatest sequence of five consecutive digits found within the number given.ExampleFollowing is the code −const num = '123546544'; const findGreatestFiveDigit = (num = '') => {    const str = num.toString();    const arr = [];    for(let i = 0; i < str.length; i++){       arr.push(str.slice(i, i + 5));    };    return Math.max(...arr); }; console.log(findGreatestFiveDigit(num));Output54654

Read More

Returning array values that are not odd in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 178 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers.Our function should construct and return a new array that contains all the numbers of the input array that are not odd.ExampleFollowing is the code −const arr = [5, 32, 67, 23, 55, 44, 23, 12]; const findNonOdd = (arr = []) => {    const res = [];    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(el % 2 !== 1){          res.push(el);          continue;       };    };    return res; }; console.log(findNonOdd(arr));Output[ 32, 44, 12 ]

Read More

Constructing a string of alternating 1s and 0s of desired length using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 613 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Starting with ‘1’ our function should construct a string of length n that contains ‘1’ and ‘0’ alternatingly.ExampleFollowing is the code −const num = 12; const buildString = (num = 1) => {    let res = '';    for(let i = 0; i < num; i++){       if(i % 2 === 0){          res += 1;       }else{          res += 0;       };    };    return res; }; console.log(buildString(num));Output101010101010

Read More

Removing least number of elements to convert array into increasing sequence using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 189 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should try and remove the least number of elements from the array so that the array becomes an increasing sequence.ExampleFollowing is the code −const arr = [1, 100, 2, 3, 100, 4, 5]; const findIncreasingArray = (arr = []) => {    const copy = arr.slice();    for(let i = 0; i < copy.length; i++){       const el = arr[i];       const next = arr[i + 1];       if(el > next){          copy[i] = undefined;       };    };    return copy.filter(Boolean); }; console.log(findIncreasingArray(arr));Output[ 1, 2, 3, 4, 5 ]

Read More

Finding the immediate next character to a letter in string using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

ProblemWe are required to write a JavaScript function that takes in a string of characters, str, and a single character, char.Our function should construct a new string that contains the immediate next character present in str after each instance of char (if any).ExampleFollowing is the code −const str = 'this is a string'; const letter = 'i'; const findNextString = (str = '', letter = '') => {    let res = '';    for(let i = 0; i < str.length; i++){       const el = str[i];       const next = str[i + 1];       if(letter === el && next){          res += next;       };    };    return res; }; console.log(findNextString(str, letter));Outputssn

Read More

Finding all possible prime pairs that sum upto input number using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 595 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should return an array of all such number pairs that when summed are n and both of them are prime.ExampleFollowing is the code −const num = 26; const isPrime = (n) => {    if (n % 2 === 0) return false;    let sqrtn = Math.sqrt(n)+1;    for (let i=3; i < sqrtn; i+=2) {       if (n % i === 0) return false;    }    return true; } const primeList = (a) => {    if (isPrime(a)) return a; ...

Read More

Finding nth element of the Padovan sequence using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 280 Views

Padovan SequenceThe Padovan sequence is the sequence of integers P(n) defined by the initial values −P(0) = P(1) = P(2) = 1and the recurrence relation,P(n) = P(n-2) + P(n-3)The first few values of P(n) are1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37, 49, 65, 86, 114, 151, 200, 265, …ProblemWe are required to write a JavaScript function that takes in a number n and return the nth term of the Padovan sequence.ExampleFollowing is the code −const num = 32; const padovan = (num = 1) => {    let secondPrev = 1, pPrev = 1, pCurr = 1, pNext = 1;    for (let i = 3; i

Read More

Mean of an array rounded down to nearest integer in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 404 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function is supposed to return the average of the given array rounded down to its nearest integer.ExampleFollowing is the code −const arr = [45, 23, 67, 68, 12, 56, 99]; const roundedMean = (arr = []) => {    const { sum, count } = arr.reduce((acc, val) => {       let { sum, count } = acc;       count++;       sum += val;       return { sum, count };    }, {       sum: 0, count: 0    });    const mean = sum / (count || 1);    return Math.round(mean); }; console.log(roundedMean(arr));OutputFollowing is the console output −53

Read More

Finding the k-prime numbers with a specific distance in a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 456 Views

K-Prime NumbersA natural number is called k-prime if it has exactly k prime factors, counted with multiplicity.Which means even though the only prime factor of 4 is 2 it will be a 2-prime number because −4 = 2 * 2 and both 2s will be counted separately taking the count to 2.Similarly, 8 is 3-prime because 8 = 2 * 2 * 2 taking the count to 3.ProblemWe are required to write a JavaScript function that takes in a number k, a distance and a range.Our function should return an array of arrays containing k-prime numbers within the range the ...

Read More
Showing 331–340 of 5,340 articles
« Prev 1 32 33 34 35 36 534 Next »
Advertisements