Found 6683 Articles for Javascript

Function that returns the minimum and maximum value of an array in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 10:23:17

473 Views

Understanding the range of values within an array is a crucial aspect of data analysis and manipulation in JavaScript. When faced with large datasets or complex algorithms, having a reliable function that can efficiently determine the minimum and maximum values of an array becomes indispensable. This article delves into the intricacies of creating a robust JavaScript function that can effortlessly extract the extreme values from an array, enabling developers to gain valuable insights and make informed decisions based on the data at hand. By exploring the step-by-step implementation and leveraging rarely used programming techniques, we empower developers to enhance their ... Read More

Finding sum of sequence upto a specified accuracy using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:40:35

105 Views

ProblemSuppose the following sequence:Seq: 1/1 , 1/1x2 , 1/1x2x3 , 1/1x2x3x4 , ....The nth term of this sequence will be −1 / 1*2*3 * ... nWe are required to write a JavaScript function that takes in a number n, and return the sum of first n terms of this sequence.ExampleFollowing is the code − Live Democonst num = 12; const seriesSum = (num = 1) => {    let m = 1;    let n = 1;    for(let i = 2; i < num + 1; i++){       m *= i;       n += (m * -1);    };    return n; }; console.log(seriesSum(num));Output-522956311

Implementing custom function like String.prototype.split() function in JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:39:53

932 Views

ProblemWe are required to write a JavaScript function that lives on the prototype object of the String class.It should take in a string separator as the only argument (although the original split function takes two arguments). And our function should return an array of parts of the string separated and split by the separator.ExampleFollowing is the code − Live Democonst str = 'this is some string'; String.prototype.customSplit = (sep = '') => {    const res = [];    let temp = '';    for(let i = 0; i < str.length; i++){       const el = str[i];     ... Read More

Implementing partial sum over an array using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:39:29

297 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 in which each corresponding element is the sum of all the elements right to it (including it) in the input array.ExampleFollowing is the code − Live Democonst arr = [5, 6, 1, 3, 8, 11]; const partialSum = (arr = []) => {    let sum = arr.reduce((acc, val) => acc + val);    const res = [];    let x = 0;    if(arr.length === 0){       return [0];    }    for(let i = 0; i

divisibleBy() function over array in JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:39:03

102 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers and a single number as two arguments.Our function should filter the array to contain only those numbers that are divisible by the number provided as second argument and return the filtered array.ExampleFollowing is the code − Live Democonst arr = [56, 33, 2, 4, 9, 78, 12, 18]; const num = 3; const divisibleBy = (arr = [], num = 1) => {    const canDivide = (a, b) => a % b === 0;    const res = arr.filter(el => {       return canDivide(el, num);    });    return res; }; console.log(divisibleBy(arr, num));Output[ 33, 9, 78, 12, 18 ]

Inverting signs of integers in an array using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:38:36

140 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers (negatives and positives).Our function should convert all positives to negatives and all negatives to positives and return the resulting array.ExampleFollowing is the code − Live Democonst arr = [5, 67, -4, 3, -45, -23, 67, 0]; const invertSigns = (arr = []) => {    const res = [];    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(+el && el !== 0){          const inverted = el * -1;          res.push(inverted);       }else{          res.push(el);       };    };    return res; }; console.log(invertSigns(arr));Output[ -5, -67, 4, -3, 45, 23, -67, 0 ]

Converting km per hour to cm per second using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:38:13

465 Views

ProblemWe are required to write a JavaScript function that takes in a number that specifies speed in kmph and it should return the equivalent speed in cm/s.ExampleFollowing is the code − Live Democonst kmph = 12; const convertSpeed = (kmph) => {    const secsInHour = 3600;    const centimetersInKilometers = 100000;    const speed = Math.floor((kmph * centimetersInKilometers) / secsInHour);    return `Equivalent in cmps is: ${speed}`; }; console.log(convertSpeed(kmph));OutputEquivalent in cmps is: 333

Binary array to corresponding decimal in JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:37:53

219 Views

ProblemWe are required to write a JavaScript function that takes in a binary array (consisting of only 0 and 1).Our function should first join all the bits in the array and then return the decimal number corresponding to that binary.ExampleFollowing is the code − Live Democonst arr = [1, 0, 1, 1]; const binaryArrayToNumber = arr => {    let num = 0;    for (let i = 0, exponent = 3; i < arr.length; i++) {       if (arr[i]) {          num += Math.pow(2, exponent);       };       exponent--;    };    return num; }; console.log(binaryArrayToNumber(arr));Output11

Finding distance between two points in a 2-D plane using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:37:06

315 Views

ProblemWe are required to write a JavaScript function that takes in two objects both having x and y property specifying two points in a plane.Our function should find and return the distance between those two points.ExampleFollowing is the code − Live Democonst a = {x: 5, y: -4}; const b = {x: 8, y: 12}; const distanceBetweenPoints = (a = {}, b = {}) => {    let distance = 0;    let x1 = a.x,    x2 = b.x,    y1 = a.y,    y2 = b.y;    distance = Math.sqrt((x2 - x1) * 2 + (y2 - y1) * 2);    return distance; }; console.log(distanceBetweenPoints(a, b));Output6.164414002968976

Finding astrological signs based on birthdates using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:33:47

1K+ Views

ProblemWe are required to write a JavaScript function that takes in a date object. And based on that object our function should return the astrological sign related to that birthdate.ExampleFollowing is the code − Live Democonst date = new Date(); // as on 2 April 2021 const findSign = (date) => {    const days = [21, 20, 21, 21, 22, 22, 23, 24, 24, 24, 23, 22];    const signs = ["Aquarius", "Pisces", "Aries", "Taurus", "Gemini", "Cancer", "Leo",    "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn"];    let month = date.getMonth();    let day = date.getDate();    if(month == 0 && day

Advertisements