Found 9316 Articles for Object Oriented Programming

Grouping objects based on key property in JavaScript

AmitDiwan
Updated on 09-Oct-2020 12:18:36

313 Views

We 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 a sample parent array −const parentArray = [[ {    key: 123, ... Read More

Fetching object keys using recursion in JavaScript

AmitDiwan
Updated on 09-Oct-2020 12:14:14

1K+ Views

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: {       fullName: 'Jay ... Read More

Array grouping on the basis of children object’s property in JavaScript

AmitDiwan
Updated on 09-Oct-2020 12:08:44

185 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

Zig-Zag pattern in strings in JavaScript?

AmitDiwan
Updated on 09-Oct-2020 12:05:07

505 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 −const 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

Building frequency map of all the elements in an array JavaScript

AmitDiwan
Updated on 09-Oct-2020 12:02:40

2K+ Views

We will be given an array of numbers / strings that contains some duplicate entries, all we have to do is to return the frequency of each element in the array.Returning an object with an element as key and its value as frequency would be perfect for this situation.We will iterate over the array with a forEach() loop and keep increasing the count of elements in the object if it already exists otherwise we will create a new property for that element in the object.And lastly, we will return the object.The full code for this problem will be −const arr ... Read More

Add line break inside a string conditionally in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:59:12

1K+ Views

We are required to write a function breakString() that takes in two arguments first the string to be broken and second is a number that represents the threshold count of characters after reaching which we have to repeatedly add line breaks in place of spaces.So, let’s do it. We will iterate over the with a for loop, we will keep a count that how many characters have elapsed with inserting a ‘’ if the count exceeds the limit and we encounter a space we replace it with line break in the new string and reset the count to 0 otherwise ... Read More

Natural Sort in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:57:34

653 Views

We have an array that contains some numbers and some strings, we are required to sort the array such that the numbers get sorted and get placed before every string and then the string should be placed sorted alphabetically.For exampleLet’s say this is our array −const arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9];The output should look like this −[1, 6, 7, 9, 47, 'afv', 'bdf', 'fdf', 'svd']Therefore, let’s write the code for this −const arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9]; const sorter = (a, b) => {    if(typeof a === ... Read More

JavaScript Strings: Replacing i with 1 and o with 0

AmitDiwan
Updated on 09-Oct-2020 11:55:05

117 Views

We are required to write a function that takes in a string as one and only argument and returns another string that has all ‘i’ and ‘o’ replaced with ‘1’ and ‘0’ respectively.It’s one of those classic for loop problems where we iterate over the string with its index and construct a new string as we move through.The code for the function will be −const string = 'Hello, is it raining in Amsterdam?'; const validate = (str) => {    let validatedString = '';    for(let i = 0; i < str.length; i++){       if(str[i] === 'a'){   ... Read More

Checking for the Gapful numbers in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:53:24

61 Views

A number is a gapful number when −It has at least three digits, andIt is exactly divisible by the number formed by putting its first and last digits togetherFor example:1053 is a gapful number because it has 4 digits and it is exactly divisible by 13. 135 is a gapful number because it has 3 digits and it is exactly divisible by 15.Our job is to write a program that returns the nearest gapful number to the number we provide as input.Let’s write the code −const n = 134; //receives a number string and returns a boolean const isGapful = ... Read More

Removing a specific substring from a string in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:51:00

331 Views

We are given a main string and a substring, our job is to create a function, let’s say removeString() that takes in these two arguments and returns a version of the main string which is free of the substring.Here, we need to remove the separator from a string, for example −this-is-a-stingLet’s now write the code for this function −const removeString = (string, separator) => {    //we split the string and make it free of separator    const separatedArray = string.split(separator);    //we join the separatedArray with empty string    const separatedString = separatedArray.join("");    return separatedString; } const str ... Read More

Advertisements