Found 9318 Articles for Object Oriented Programming

Thrice sum of elements of array - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:53:52

136 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of three consecutive elements from the original array.For example, if the input array is −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]Then the output should be −const output = [3, 12, 21, 9];ExampleFollowing is the code −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] const thriceSum = arr => {    if(!arr.length){       return [];    }    const res = [];    for(let i = 0; i < arr.length; i += 3){       res.push(arr[i] + (arr[i+1] || 0) + (arr[i+2] || 0));    };    return res; }; console.log(thriceSum(arr));OutputThis will produce the following output in console −[ 3, 12, 21, 9 ]

Does this array contain any majority element - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:52:54

84 Views

Given an array of Numbers, any element of the array will be a majority element if that element appears more than array length's 1/2 times in the array.For example −If the length of array is 7, Then if there's any element in the array that appears for at least 4 number of times, it will be considered a majority. And it’s quite apparent that any particular array can have at most one majority element.We are required to write a JavaScript function that takes in an array of numbers with repetitive values and returns true if there exists a majority element ... Read More

Are array of numbers equal - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:51:19

84 Views

We are required to write a JavaScript function that takes in two arrays of Numbers, say first and second and checks for their equality.The arrays will be considered equal if −They contain the same elements and in the same order.The product of all the elements of the first array and second array is equal.The first array of numbers −const first = [3, 5, 6, 7, 7];The second array of numbers −const second = [7, 5, 3, 7, 6];ExampleFollowing is the code −const first = [3, 5, 6, 7, 7]; const second = [7, 5, 3, 7, 6]; const isEqual = ... Read More

Check whether a string ends with some other string - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:50:14

110 Views

We are required to write a JavaScript function that takes in two strings say, str1 and str2. The function should determine whether or not str1 ends with str2. Our function should return a boolean on this basis.Here’s our 1st string −const str1 = 'this is just an example';Here’s our 2nd string −const str2 = 'ample';ExampleFollowing is the code −const str1 = 'this is just an example'; const str2 = 'ample'; const endsWith = (str1, str2) => {    const { length } = str2;    const { length: l } = str1;    const sub = str1.substr(l - length, length); ... Read More

Finding special type of numbers - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:49:05

126 Views

In the decimal number system, all the real numbers can be divided into two groups −Rational NumbersIrrational NumbersFor the scope of this problem we will only discuss the rational numbers, All those numbers which can be written in the p/q (where q !== 0) form are called rational numbers.Like 14, 4.6, 3.33333... and many moreThe rational numbers, further can be divided into two groups −Terminating decimal numbersRepeating decimal numbersThis categorization is made on the basis of result obtained upon dividing p by q.The thumb for this categorization is that −We will obtain a terminating decimal number if and only if ... Read More

JavaScript: Finding nearest prime number greater or equal to sum of digits - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:47:58

113 Views

We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum.ExampleFollowing is the code −const num = 56563; const digitSum = (num, sum = 0) => {    if(num){       return digitSum(Math.floor(num / 10), sum + (num % 10));    }    return sum; }; const isPrime = n => {    if (n===1){       return false;    }else if(n === 2){       return true;    }else{       for(let ... Read More

Converting a JavaScript object to an array of values - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:46:12

194 Views

We are required to create an array out of a JavaScript object, containing the values of all of the object's properties. For example, given this object −{    "firstName": "John",    "lastName": "Smith",    "isAlive": "true",    "age": "25" } We have to produce this array −const myarray = ['John', 'Smith', 'true', '25'];ExampleFollowing is the code −Solution1const obj = {    "firstName": "John",    "lastName": "Smith",    "isAlive": "true",    "age": "25" }; const objectToArray = obj => {    const keys = Object.keys(obj);    const res = [];    for(let i = 0; i < keys.length; i++){       ... Read More

Retrieve user id from array of object - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:44:49

736 Views

Suppose, we have an array of objects where the user names are mapped to some unique ids like this −const arr = [    {"4": "Rahul"},    {"7": "Vikram"},    {"6": "Rahul"},    {"3": "Aakash"},    {"5": "Vikram"} ];As apparent in the array, same names can have more than one ids but same ids can be used to map two different names.We are required to write a JavaScript function that takes in one such array as the first argument and a name string as the second argument. The function should return an array of all ids that were used to ... Read More

Find the shortest string in an array - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:43:24

617 Views

We are required to write a JavaScript function that takes in an array of strings and returns the index of string that is shortest in length.We will simply use a for loop and persist the index of string which is shortest in length.ExampleFollowing is the code −const arr = ['this', 'can', 'be', 'some', 'random', 'sentence']; const findSmallest = arr => {    const creds = arr.reduce((acc, val, index) => {       let { ind, len } = acc;       if(val.length < len){          len = val.length;          ind = index; ... Read More

Sum all similar elements in one array - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:41:53

355 Views

We are required to write a JavaScript function that takes in an array of Numbers and sums all the identical numbers together at one indexFor example −If the input array is −const arr = [20, 10, 15, 20, 15, 10];Then the output should be −const output = [40, 20, 30];ExampleFollowing is the code −const arr = [20, 10, 15, 20, 15, 10]; const addSimilar = arr => {    for(let i = 0; i < arr.length; i++){       while(i !== arr.lastIndexOf(arr[i])){          const ind = arr.lastIndexOf(arr[i]);          arr[i] += arr.splice(ind, 1)[0];       };    }; }; addSimilar(arr); console.log(arr);OutputThis will produce the following output in console −[ 40, 20, 30 ]

Advertisements