Isosceles Triangles with Nearest Perimeter Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:30:10

201 Views

Almost Isosceles TriangleAn Almost Isosceles Integer Triangle is a triangle that all its side lengths are integers and also, two sides are almost equal, being their absolute difference 1 unit of length.ProblemWe are required to write a JavaScript function that takes in a number which specifies the perimeter of a triangle.Our function should find the measurement of such an almost isosceles triangle whose perimeter is nearest to the input perimeter.For example, if the desired perimeter is 500, Then the almost isosceles triangle with the nearest perimeter will be − [105, 104, 181]ExampleFollowing is the code − Live Democonst perimeter = 500; ... Read More

Maximum Product of Any Two Adjacent Elements in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:29:43

661 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers.Our function should find the maximum product obtained from multiplying 2 adjacent numbers in the array.ExampleFollowing is the code − Live Democonst arr = [9, 5, 10, 2, 24, -1, -48]; function adjacentElementsProduct(array) {    let maxProduct = array[0] * array[1];    for (let i = 1; i < array.length; i++) {       product = array[i] * array[i + 1];       if (product > maxProduct)          maxProduct = product;    }    return maxProduct; }; console.log(adjacentElementsProduct(arr));Output50

Find Sum of Remaining Numbers to Reach Target Average Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:29:21

166 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers and a single number.Our function should find that very number which should be pushed to the array so that its average equals the number specified by the second argument.ExampleFollowing is the code − Live Democonst arr = [4, 20, 25, 17, 9, 11, 15]; const target = 25; function findNumber(arr, target) {    let sum = arr.reduce((a, b) => a + b, 0);    let avg = sum / arr.length;    let next = Math.ceil((target * (arr.length + 1)) - sum);    if (next

Finding Nth Element of an Increasing Sequence Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:28:04

250 Views

ProblemConsider an increasing sequence which is defined as follows −The number seq(0) = 1 is the first one in seq.For each x in seq, then y = 2 * x + 1 and z = 3 * x + 1 must be in seq too.There are no other numbers in seq.Therefore, the first few terms of this sequence will be −[1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...]We are required to write a function that takes in a number n and returns the nth term of this sequence.ExampleFollowing is the code − Live Democonst num = ... Read More

Reversing Negative and Positive Numbers in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:27:34

1K+ Views

ProblemWe are required to write a JavaScript function that takes in a number and returns its reversed number.One thing that we should keep in mind is that numbers should preserve their sign; i.e., a negative number should still be negative when reversed.ExampleFollowing is the code − Live Democonst num = -224; function reverseNumber(n) {    let x = Math.abs(n)    let y = 0    while (x > 0) {       y = y * 10 + (x % 10)       x = Math.floor(x / 10)    };    return Math.sign(n) * y }; console.log(reverseNumber(num));Output-422

Boolean Gates in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:27:09

1K+ Views

ProblemWe are required to write a JavaScript function that takes in an array of Boolean values and a logical operator.Our function should return a Boolean result based on sequentially applying the operator to the values in the array.ExampleFollowing is the code − Live Democonst array = [true, true, false]; const op = 'AND'; function logicalCalc(array, op){    var result = array[0];    for(var i = 1; i < array.length; i++){       if(op == "AND"){          result = result && array[i];       }       if(op == "OR"){          result = result || array[i];       }       if(op == "XOR"){          result = result != array[i];       }    }    return result; } console.log(logicalCalc(array, op));Outputfalse

Displaying Likes on a Post Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:26:44

281 Views

ProblemWe are required to write a JavaScript function that takes in an array of names (string). This array specifies the names of people that liked a particular post on some social networking site.If the count of likes are less than or equal to three our function should simply return all names saying these people liked the post but if the count is greater than three then our function should return first two names and remaining count.ExampleFollowing is the code − Live Democonst names = ['Ram', 'Manohar', 'Jay', 'Kumar', 'Vishal']; const displayLikes = (names) => {    return [       ... Read More

Find Greatest and Smallest Number in a Space-Separated String using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:25:08

311 Views

ProblemWe are required to write a JavaScript function that takes in a string that contains numbers separated by spaces.Our function should return a string that contains only the greatest and the smallest number separated by space.Inputconst str = '5 57 23 23 7 2 78 6';Outputconst output = '78 2';Because 78 is the greatest and 2 is the smallest.ExampleFollowing is the code − Live Democonst str = '5 57 23 23 7 2 78 6'; const pickGreatestAndSmallest = (str = '') => {    const strArr = str.split(' ');    let creds = strArr.reduce((acc, val) => {    let { greatest, ... Read More

Returning Lengthy Words from a String Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:24:04

114 Views

ProblemWe are required to write a JavaScript function that takes in a sentence of words and a number. The function should return an array of all words greater than the length specified by the number.Inputconst str = 'this is an example of a basic sentence'; const num = 4;Outputconst output = [ 'example', 'basic', 'sentence' ];Because these are the only three words with length greater than 4.ExampleFollowing is the code − Live Democonst str = 'this is an example of a basic sentence'; const num = 4; const findLengthy = (str = '', num = 1) => {    const strArr ... Read More

Numbers Obtained During Checking Divisibility by 7 Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:22:30

640 Views

ProblemWe can check for a number to be divisible by 7 if it is of the form 10a + b and a - 2b is divisible by 7.We continue to do this until a number known to be divisible by 7 is obtained; we can stop when this number has at most 2 digits because we are supposed to know if a number of at most 2 digits is divisible by 7 or not.We are required to write a JavaScript function that takes in a number and return the number of such steps required to reduce the number to a ... Read More

Advertisements