Finding Distance Between Two Points in a 2D Plane Using JavaScript

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

400 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

Fit Remaining Passengers in the Bus Using JavaScript

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

271 Views

ProblemWe are required to write a JavaScript function that takes in three parameters −cap − is the amount of people the bus can hold excluding the driver.on − is the number of people on the bus excluding the driver.wait − is the number of people waiting to get on to the bus excluding the driver.If there is enough space, we should return 0, and if there isn't, we should return the number of passengers we can't take.ExampleFollowing is the code − Live Democonst cap = 120; const on = 80; const wait = 65; const findCapacity = (cap, on, wait) => ... Read More

DNA to RNA Conversion Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:25:34

864 Views

DNA And RNA RelationDeoxyribonucleic acid, DNA is the primary information storage molecule in biological systems. It is composed of four nucleic acid bases Guanine ('G'), Cytosine ('C'), Adenine ('A'), and Thymine ('T').Ribonucleic acid, RNA, is the primary messenger molecule in cells. RNA differs slightly from DNA its chemical structure and contains no Thymine. In RNA Thymine is replaced by another nucleic acid Uracil ('U').ProblemWe are required to write a JavaScript function which translates a given DNA string into RNA.ExampleFollowing is the code − Live Democonst DNA = 'GCAT'; const DNAtoRNA = (DNA) => {    let res = '';    for(let ... Read More

Returning Array of Natural Numbers Between a Range in JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:24:41

187 Views

ProblemWe are required to write a JavaScript function that takes in an array of two numbers [a, b] (a {    if(lower > upper){       return [];    };    const res = [];    for(let i = lower; i

Returning the Nth Even Number Using JavaScript

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

597 Views

ProblemWe are required to write a JavaScript function that takes in a number n. And our function should simply return the nth even number in natural numbers.ExampleFollowing is the code − Live Democonst num = 67765; const nthEven = (num = 1) => {    const next = num * 2;    const res = next - 2;    return res; }; console.log(nthEven(num));Output135528

Replace Digits to Form Binary Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:23:04

547 Views

ProblemWe are required to write a JavaScript function that takes in a string of digits. Our function should replace any digit below 5 with '0' and any digit 5 and above with '1' and return the resulting string.ExampleFollowing is the code − Live Democonst str = '262355677834342'; const convert = (str = '') => {    let res = '';    for(let i = 0; i < str.length; i++){       const el = +str[i];       if(el < 5){          res += 0;       }else{          res += 1;       };    };    return res; }; console.log(convert(str));Output010011111100000

Check Existence of All Continents in Array of Objects in JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:19:26

141 Views

ProblemWe are required to write a JavaScript function that takes in an array of objects that contains data about the continent of belonging for some people.Our function should return true if it finds six different continents in the array of objects, false otherwise.ExampleFollowing is the code − Live Democonst people = [ { firstName: 'Dinesh', lastName: 'A.', country: 'Algeria', continent: 'Africa', age: 25, language: 'JavaScript' }, { firstName: 'Ishan', lastName: 'M.', ... Read More

Finding Average Age from Array of Objects in JavaScript

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

1K+ Views

ProblemWe are required to write a JavaScript function that takes in an object that contains data about some people.Our function should simply find the average of the age property for those people.ExampleFollowing is the code − Live Democonst people = [    { fName: 'Ashish', age: 23 },    { fName: 'Ajay', age: 21 },    { fName: 'Arvind', age: 26 },    { fName: 'Mahesh', age: 28 },    { fName: 'Jay', age: 19 }, ]; const findAverageAge = (arr = []) => {    const { sum, count } = arr.reduce((acc, val) => {       let { sum, count } = acc;       sum += val.age;       count++;       return { sum, count };       }, {          sum: 0, count: 0    });    return (sum / (count || 1)); }; console.log(findAverageAge(people));Output23.4

Summing Array of String Numbers Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:16:43

738 Views

ProblemWe are required to write a JavaScript function that takes in an array that contains integers and string numbers.Our function should sum all the integers and string numbers together to derive a new number and return that number.ExampleFollowing is the code − Live Democonst arr = [67, 45, '34', '23', 4, 6, '6']; const mixedSum = (arr = []) => {    let sum = 0;    for(let i = 0; i < arr.length; i++){       const el = arr[i];       sum += +el;    };    return sum; }; console.log(mixedSum(arr));Output185

Advertisements