Found 6683 Articles for Javascript

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

AmitDiwan
Updated on 20-Apr-2021 07:41:14

471 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 − Live Democonst 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

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

AmitDiwan
Updated on 20-Apr-2021 07:39:03

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 − Live Democonst 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

Create palindrome by changing each character to neighboring character in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:36:53

376 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 − Live Democonst str = 'adfa'; const canFormPalindrome = (str = '') => {    const middle = str.length / 2;    for(let i = 0; i < ... Read More

Finding value of a sequence for numbers in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:33:42

482 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 − Live Democonst n = 12; const p = 70; const findSeqSum = (n, p) => {    let sum = 0;    for(let k = 0; k

Finding top three most occurring words in a string of text in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:30:15

367 Views

ProblemWe are required to write a JavaScript function that takes in a English alphabet string. Our function should return the top three most frequent words present in the string.ExampleFollowing is the code − Live Democonst str = 'Python was developed by Guido van Rossum in the late eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the Netherlands. Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol-68, SmallTalk, and Unix shell and other scripting languages. Python is copyrighted. Python source code is now available under the GNU General Public License (GPL)'; ... Read More

Removing n characters from a string in alphabetical order in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:26:45

224 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 − Live Democonst 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 two prime numbers with a specific number gap in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:25:13

160 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 − Live Democonst gap = 4; const range = [20, 200]; const primesInRange = (gap, [left, right]) => {    const isPrime = num => {       for(let i = 2; i < num; i++){          if(num % i === ... Read More

Finding two missing numbers that appears only once and twice respectively in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:22:59

102 Views

ProblemWe are required to write a JavaScript function that takes in an array in which all the numbers appear thrice except one which appears twice and one which appears only one. Our function should find and return these two numbers.ExampleFollowing is the code − Live Democonst arr = [1, 1, 1, 2, 2, 3]; const findMissing = (arr = []) => {    let x = 0;    let y = 0;    for(let i = 0; i < arr.length; i++){       if(arr.filter(a => a === arr[i]).length === 2){          y = arr[i];       };       if(arr.filter(b => b === arr[i]).length === 1){          x = arr[i];       };    };    return [x, y]; }; console.log(findMissing(arr));OutputFollowing is the console output −[3, 2]

Creating a Projectile class to calculate height horizontal distance and landing in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:18:23

91 Views

ProblemWe are required to write a JavaScript class, Projectile, which takes in 3 arguments when initialized −starting height (0 ≤ h0 < 200)starting velocity (0 < v0 < 200)angle of the projectile when it is released (0° < a < 90°, measured in degrees).We need to write the following method for the Projectile class.A horiz method, which also takes in an argument t and calculates the horizontal distance that the projectile has traveled. [takes in a double, returns a double]ExampleThe code for this class will be − Live Democlass Projectile{    constructor(h, u, ang){       this.h = h;   ... Read More

Cutting off number at each digit to construct an array in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:06:13

60 Views

ProblemWe are required to write a JavaScript function that takes in a number. Our function should return an array of strings containing the number cut off at each digit.ExampleFollowing is the code − Live Democonst num = 246; const cutOffEach = (num = 1) => {    const str = String(num);    const res = [];    let temp = '';    for(let i = 0; i < str.length; i++){       const el = str[i];       temp += el;       res.push(temp);    };    return res; }; console.log(cutOffEach(num));OutputFollowing is the console output −[ '2', '24', '246' ]

Advertisements