Found 9316 Articles for Object Oriented Programming

Filtering out the non-unique value to appear only once in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:17:46

119 Views

We have an array of literals that contains some duplicate values appearing for many times like this −const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4];We are required to write a JavaScript function that takes in this array and pick out all the duplicate entries from the original array and only once.Therefore, for the above array, the output should be −const output = [1, 4, 3, 2];ExampleThe code for this will be −const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4]; const removeDuplicate = arr => {   ... Read More

Finding Armstrong numbers in a given range in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:15:40

408 Views

A number is called Armstrong number if the following equation holds true for that number: xy...z =xx +yy+...+zz, where n denotes the number of digits in the number.For example:153 is an Armstrong number because −11 +55 +33 = 1 + 125 + 27 =153We are required to write a JavaScript function that takes in two numbers, a range, and returns all the numbers between them that are Armstrong numbers (including them, if they are Armstrong)ExampleThe code for this will be −const isArmstrong = number => {    let num = number;    const len = String(num).split("").length;    let res = ... Read More

Sorting arrays using bubble sort in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:13:52

268 Views

We are required to write a JavaScript function that takes in an array of literals and sorts it using bubble sort.ExampleThe code for this will be −const arr = [4, 56, 4, 23, 8, 4, 23, 2, 7, 8, 8, 45]; const swap = (items, firstIndex, secondIndex) => {    var temp = items[firstIndex];    items[firstIndex] = items[secondIndex];    items[secondIndex] = temp; }; const bubbleSort = items => {    var len = items.length,    i, j;    for (i=len-1; i >= 0; i--){       for (j=len-i; j >= 0; j--){          if (items[j] < items[j-1]){             swap(items, j, j-1);          }       }    }    return items; }; console.log(bubbleSort(arr));OutputThe output in the console −[    2, 4, 4, 4, 7,    8, 8, 8, 23, 23,    45, 56 ]

Splitting Number into k length array in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:12:13

168 Views

We are required to write a JavaScript function that takes in two numbers say m and k, and returns an array of size k with all the elements of the resulting array adding up to m.ExampleThe code for this will be −const len = 30; const sum = 121; const splitNumber = (len, sum) => {    const res = [];      for(let i = 0; i < len; i++){       res.push(sum / len);    };    return res; }; console.log(splitNumber(len, sum));OutputThe output in the console −[    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333 ]

Checking for special type of Arrays in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:10:17

80 Views

We are required to write a JavaScript function that takes in an array of literals and checks if elements are the same or not if read from front or back. Such arrays are also known by the name of palindrome arrays.Some examples of palindrome arrays are −const arr1 = [‘a’, ‘b’, ‘c’, ‘b’, ‘a’]; const arr2 = [4, 7, 7, 4]; const arr3 = [7, 7, 7, 7, 7, 7];ExampleThe code for this will be −const arr = [1, 5, 7, 4, 15, 4, 7, 5, 1]; const isPalindrome = arr => {    const { length: l } = arr;    const mid = Math.floor(l / 2);    for(let i = 0; i

Merge and remove duplicates in JavaScript Array

AmitDiwan
Updated on 15-Oct-2020 09:08:52

303 Views

Suppose, we have two arrays of literals like these −const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6];We are required to write a JavaScript function that takes in two such arrays and returns a new array with all the duplicates removed (should appear only once).ExampleThe code for this will be −const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6]; const mergeArrays = (first, second) => {    const { length: l1 } = first;    const { length: ... Read More

Greater possible digit difference of a number in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:07:04

94 Views

We are required to write a JavaScript function that takes in a number. Then the function should return the greatest difference that exists between any two digits of the number.In other words, the function should simply return the difference between the greatest and the smallest digit present in it.For example:If the number is 654646, Then the smallest digit here is 4 and the greatest is 6 Hence, our output should be 2ExampleThe code for this will be −const num = 654646; const maxDifference = (num, min = Infinity, max = -Infinity) => {    if(num){       const digit ... Read More

Picking all the numbers present in a string in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:03:15

102 Views

We are required to write a JavaScript function that takes in a string that contains some one-digit numbers in between and the function should return the sum of all the numbers present in the string.ExampleThe code for this will be −const str = 'uyyudfgdfgf5jgdfj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; const sumNum = str => {    const strArr = str.split("");    let res = 0;    for(let i = 0; i < strArr.length; i++){       if(+strArr[i]){          res += +strArr[i];       };    };    return res; }; console.log(sumNum(str));OutputThe output in the console −35

Addition multiplication ladder in an array in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:00:59

105 Views

We are required to write a JavaScript function that takes in an array of numbers and returns the alternative multiplicative sum of the elements.For example: If the array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be calculated like this −1*2+3*4+5*6+7 2+12+30+7And the output should be −51Let’s write the code for this function −ExampleThe code for this will be −const arr = [1, 2, 3, 4, 5, 6, 7]; const alternateOperation = arr => {    const productArr = arr.reduce((acc, val, ind) => {       if(ind % 2 === 1){          return acc;       };       acc.push(val * (arr[ind + 1] || 1));       return acc;    }, []);    return productArr.reduce((acc, val) => acc + val); }; console.log(alternateOperation(arr));OutputThe output in the console −51

Object to Map conversion in JavaScript

AmitDiwan
Updated on 15-Oct-2020 08:59:02

131 Views

Suppose, we have an object like this −const obj = {    name: "Jai",    age: 32,    occupation: "Software Engineer",    address: "Dhindosh, Maharasthra",    salary: "146000" };We are required to write a JavaScript function that takes in such an object with key value pairs and converts it into a Map.ExampleThe code for this will be −const obj = {    name: "Jai",    age: 32,    occupation: "Software Engineer",    address: "Dhindosh, Maharasthra",    salary: "146000" }; const objectToMap = obj => {    const keys = Object.keys(obj);    const map = new Map();    for(let i = ... Read More

Advertisements