Web Development Articles

Page 40 of 801

Finding two prime numbers with a specific number gap in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 261 Views

ProblemWe are required to write a JavaScript function that takes in a number, gap as the first argument and a range array of two numbers as the second argument. Our function should return an array of all such prime pairs that have an absolute difference of gap and falls between the specified range.ExampleFollowing is the code −const gap = 4; const range = [20, 200]; const primesInRange = (gap, [left, right]) => {    const isPrime = num => {       for(let i = 2; i < num; i++){          if(num % i === 0){ ...

Read More

Removing n characters from a string in alphabetical order in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 331 Views

ProblemWe are required to write a JavaScript function that takes in a lowercase alphabet string and number num.Our function should remove num characters from the array in alphabetical order. It means we should first remove ‘a’ if they exist then ‘b’ , ‘c’ and so on until we hit the desired num.ExampleFollowing is the code −const str = 'abascus'; const num = 4; const removeAlphabetically = (str = '', num = '') => {    const legend = "abcdefghijklmnopqrstuvwxyz";    for(let i = 0; i < legend.length; i+=1){       while(str.includes(legend[i]) && num > 0){         ...

Read More

Finding value of a sequence for numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 605 Views

ProblemConsider the following sequence sum −$$seq(n,\:p)=\displaystyle\sum\limits_{k=0} \square(-1)^{k}\times\:p\:\times 4^{n-k}\:\times(\frac{2n-k}{k})$$We are required to write a JavaScript function that takes in the numbers n and p returns the value of seq(n, p).ExampleFollowing is the code −const n = 12; const p = 70; const findSeqSum = (n, p) => {    let sum = 0;    for(let k = 0; k

Read More

Create palindrome by changing each character to neighboring character in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 482 Views

ProblemWe are required to write a JavaScript function that takes in a string. Our function can do the following operations on the string −each character MUST be changed either to the one before or the one after in the alphabet."a" can only be changed to "b" and "z" to "y".Our function should return True if at least one of the outcomes of these operations is a palindrome or False otherwise.ExampleFollowing is the code −const str = 'adfa'; const canFormPalindrome = (str = '') => {    const middle = str.length / 2;    for(let i = 0; i < middle; ...

Read More

Constructing full name from first name, last name and optional middle name in JavaScript

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

ProblemWe are required to write a JavaScript function that takes in three strings, first string specifies the first name, second string specifies the last name and the third optional string specifies the middle name.Our function should return the full name based on these inputs.ExampleFollowing is the code −const firstName = 'Vijay'; const lastName = 'Raj'; const constructName = (firstName, lastName, middleName) => {    if(!middleName){       middleName = '';    };    let nameArray = [firstName, middleName, lastName];    nameArray = nameArray.filter(Boolean);    return nameArray.join(' '); }; console.log(constructName(firstName, lastName));OutputFollowing is the console output −Vijay Raj

Read More

Finding the height based on width and screen size ratio (width:height) in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 710 Views

ProblemWe are required to write a JavaScript function that takes in the width of the screen as the first argument and the aspect ratio (w:h) as the second argument. Based on these two inputs our function should return the height of the screen.ExampleFollowing is the code −const ratio = '18:11'; const width = 2417; const findHeight = (ratio = '', width = 1) => {    const [w, h] = ratio    .split(':')    .map(Number);    const height = (width * h) / w;    return Math.round(height); }; console.log(findHeight(ratio, width));OutputFollowing is the console output −1477

Read More

Realtime moving average of an array of numbers in JavaScript

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

ProblemWe are required to write a JavaScript function that takes in an array. Our function should construct a new array that stores the moving average of the elements of the input array. For instance −[1, 2, 3, 4, 5] → [1, 1.5, 3, 5, 7.5]First element is the average of the first element, the second element is the average of the first 2 elements, the third is the average of the first 3 elements and so on.ExampleFollowing is the code −const arr = [1, 2, 3, 4, 5]; const movingAverage = (arr = []) => {    const res = ...

Read More

Reducing array elements to all odds in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 183 Views

ProblemWe are required to write a JavaScript function that takes in an array. Our function should change the array numbers like this −If the number is odd, leave it changed.If the number is even, subtract 1 from it.And we should return the new array.ExampleFollowing is the code −const arr = [5, 23, 6, 3, 66, 12, 8]; const reduceToOdd = (arr = []) => {    const res = [];    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(el % 2 === 1){          res.push(el);       }else{          res.push(el - 1);       };    };    return res; }; console.log(reduceToOdd(arr));OutputFollowing is the console output −[ 5, 23, 5, 3, 65, 11, 7 ]

Read More

All right triangles with specified perimeter in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 172 Views

ProblemWe are required to write a JavaScript function that takes in a number that specifies the perimeter for a triangle. Our function should return an array of all the triangle side triplets whose perimeter is same as specified by the input.ExampleFollowing is the code −const perimeter = 120; const findAllRight = (perimeter = 1) => {    const res = [];    for(let a = 1; a

Read More

Return the nearest greater integer of the decimal number it is being called on in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 281 Views

ProblemWe are required to write a JavaScript function that lives in the Math class of JavaScript.Our function should return the nearest greater integer of the decimal number it is being called on.If the number is already an integer, we should return it as it is.ExampleFollowing is the code −const num = 234.56; Math.ceil = function(num){    if(typeof num !== 'number'){       return NaN;    };    if(num % 1 === 0){       return num;    };    const [main] = String(num).split('.');      return +main + 1; }; console.log(Math.ceil(num));OutputFollowing is the console output −235

Read More
Showing 391–400 of 8,006 articles
« Prev 1 38 39 40 41 42 801 Next »
Advertisements