Completely Remove Duplicate Items from an Array in JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:07:57

297 Views

We are required to write a function that takes in an array and returns a new array that have all duplicate values removed from it.The values that appeared more than once in the original array should not even appear for once in the new array.For example, if the input is −const arr = [23, 545, 43, 232, 32, 43, 23, 43];The output should be −const output = [545, 232, 32];Understanding the difference −Array.prototype.indexOf() → It returns the index of first occurrence of searched string if it exists, otherwise -1.Array.prototype.lastIndexOf() → It returns the index of last occurrence of searched string ... Read More

Convert Number to Alphabet Letter in JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:07:07

2K+ Views

We are required to write a function that takes in a number between 1 and 26 (both inclusive) and returns the corresponding English alphabet for it. (capital case) If the number is out of this range return -1.For example −toAlpha(3) = C toAlpha(18) = RAnd so on.The ASCII CodesASCII codes are the standard numerical representation of all the characters and numbers present on our keyboard and many for.The capital English alphabets are also mapped in the ascii char codes, they start from 65 and goes all the way up to 90, with 65 being the value for ‘A’, 66 for ... Read More

Get Property of Difference Between Two Objects in JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:05:48

4K+ Views

Let’s say, we are given two objects that have similar key value pairs with one or key having different values in both objects. Our job is to write a function that takes in the two objects as argument and returns the very first key it finds having different values. If all the keys have exact same values, it should return -1.Here are the sample objects −const obj1 = {    name: 'Rahul Sharma',    id: '12342fe4554ggf',    isEmployed: true,    age: 45,    salary: 190000,    job: 'Full Stack Developer',    employedSince: 2005 } const obj2 = {    name: ... Read More

Make a List of Partial Sums Using forEach in JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:04:11

291 Views

We have an array of numbers like this −const arr = [1, 1, 5, 2, -4, 6, 10];We are required to write a function that returns a new array, of the same size but with each element being the sum of all elements until that point.So, the output should look like −const output = [1, 2, 7, 9, 5, 11, 21];Let’s write the function partialSum(). The full code for this function will be −Exampleconst arr = [1, 1, 5, 2, -4, 6, 10]; const partialSum = (arr) => {    const output = [];    arr.forEach((num, index) => {   ... Read More

Recursive Sum of All Digits in a Number using JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:02:55

2K+ Views

Let’s say, we are required to create a function that takes in a number and finds the sum of its digits recursively until the sum is a one-digit number.For example −findSum(12345) = 1+2+3+4+5 = 15 = 1+5 = 6So, the output should be 6.Let’s write the code for this function findSum() −Example// using recursion const findSum = (num) => {    if(num < 10){       return num;    }    const lastDigit = num % 10;    const remainingNum = Math.floor(num / 10);    return findSum(lastDigit + findSum(remainingNum)); } console.log(findSum(2568));We check if the number is less than 10, ... Read More

Sum Elements at the Same Index in Array of Arrays in JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:01:48

1K+ Views

We have an array of arrays and are required to write a function that takes in this array and returns a new array that represents the sum of corresponding elements of original array.If the original array is −[    [43, 2, 21], [1, 2, 4, 54], [5, 84, 2], [11, 5, 3, 1] ]Then the output should be −[60, 93, 30, 55]Let’s write a sample function addArray()The full code for this function will be −Exampleconst arr = [    [43, 2, 21], [1, 2, 4, 54], [5, 84, 2], [11, 5, 3, 1] ]; const sumArray = (array) => { ... Read More

Group Objects Inside a Nested Array in JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:00:09

2K+ Views

Let’s say e have a parentArray that contains many sub arrays each of the same size, each sub array is an array of objects containing two properties: key and value. Within a subarray it is confirmed that two objects cannot have the same key but all subarrays have the same pair of n keys where n is the size of the sub array.Our job is to prepare an object with key as key of objects and value being an array that contains all the values for that particular key.Here is our sample parent array −const parentArray = [[    { ... Read More

Recursively List Nested Object Keys in JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:57:39

6K+ Views

Let’s say, we have an object with other objects being its property value, it is nested to 2-3 levels or even more.Here is the sample object −const people = {    Ram: {       fullName: 'Ram Kumar',       details: {          age: 31,          isEmployed: true       }    },    Sourav: {       fullName: 'Sourav Singh',       details: {          age: 22,          isEmployed: false       }    },    Jay: {       ... Read More

Grouping on the Basis of Object Property in JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:55:56

143 Views

We have an array of objects that contains data about some cars. The array is given as follows −const cars = [{    company: 'Honda',    type: 'SUV' }, {    company: 'Hyundai',    type: 'Sedan' }, {    company: 'Suzuki',    type: 'Sedan' }, {    company: 'Audi',    type: 'Coupe' }, {    company: 'Tata',    type: 'SUV' }, {    company: 'Morris Garage',    type: 'Hatchback' }, {    company: 'Honda',    type: 'SUV' }, {    company: 'Tata',    type: 'Sedan' }, {    company: 'Honda',    type: 'Hatchback' }];We are required to write a program ... Read More

Convert Odd and Even Indexed Characters to Uppercase and Lowercase in JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:53:57

2K+ Views

We need to write a function that reads a string and converts the odd indexed characters in the string to upperCase and the even ones to lowerCase and returns a new string.Full code for doing the same will be −Exampleconst text = 'Hello world, it is so nice to be alive.'; const changeCase = (str) => {    const newStr = str    .split("")    .map((word, index) => {       if(index % 2 === 0){          return word.toLowerCase();       }else{          return word.toUpperCase();       }    })   ... Read More

Advertisements