Found 9321 Articles for Object Oriented Programming

Add number strings without using conversion library methods in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:22:01

106 Views

We are required to write a JavaScript function that takes in two number strings. The function should add the numbers in the string without actually converting them to numbers or using any other conversion library methods.For example −If the input strings are −const str1 = '123'; const str2 = '456';Then the output should be −const output = '579';ExampleThe code for this will be −const str1 = '123'; const str2 = '456'; const addStrings = (num1, num2) => {    // Let's make sure that num1 is not shorter than num2    if (num1.length < num2.length) {       let ... Read More

Iterate through Object keys and manipulate the key values in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:19:56

183 Views

Suppose, we have an array of objects like this −const arr = [    {       col1: ["a", "b"],       col2: ["c", "d"]    },    {       col1: ["e", "f"],       col2: ["g", "h"]    } ];We are required to write a JavaScript function that takes in one such array and returns the following output.const output = [    {       col1: "b",       col2: "d"    },    {       col1: "f",       col2: "h"    } ];Basically, we want to convert ... Read More

Function to check two strings and return common words in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:17:17

632 Views

We are required to write a JavaScript function that takes in two strings as arguments. The function should then check the two strings for common characters and prepare a new string of those characters.Lastly, the function should return that string.The code for this will be −Exampleconst str1 = "IloveLinux"; const str2 = "weloveNodejs"; const findCommon = (str1 = '', str2 = '') => {    const common = Object.create(null);    let i, j, part;    for (i = 0; i < str1.length - 1; i++) {       for (j = i + 1; j

Filter an array containing objects based on another array containing objects in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:36:43

12K+ Views

Suppose we have two arrays of objects like these −const arr1 = [{id:'1', name:'A'}, {id:'2', name:'B'}, {id:'3', name:'C'}, {id:'4', name:'D'}]; const arr2 = [{id:'1', name:'A', state:'healthy'}, {id:'3', name:'C', state:'healthy'}];We are required to write a JavaScript function that takes in two such arrays. Our function should return a new filtered version of the first array (arr1 in this case) that contains only those objects with a name property that are not contained in the second array (arr2 in this case) with the same name property.Therefore, the output, in this case, should look like −const output = [{id:'2', name:'B'}, {id:'4', name:'D'}];ExampleThe code ... Read More

How to sort an object in ascending order by the value of a key in JavaScript?

AmitDiwan
Updated on 20-Nov-2020 10:34:47

683 Views

Suppose we have the following object −const obj = {    "sub1": 56,    "sub2": 67,    "sub3": 98,    "sub4": 54,    "sub5": 87 };We are required to write a JavaScript function that takes in one such object. Then our function should sort the object in ascending order of the values present in the object. And then at last, we should return the object thus formed.ExampleThe code for this will be −const obj = {    "sub1": 56,    "sub2": 67,    "sub3": 98,    "sub4": 54,    "sub5": 87 }; const sortObject = obj => {    const ... Read More

Extract arrays separately from array of Objects in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:33:19

1K+ Views

Suppose, we have an array of objects like this −const arr = [{    name : 'Client 1',    total: 900,    value: 12000 }, {    name : 'Client 2',    total: 10,    value: 800 }, {    name : 'Client 3',    total: 5,    value : 0 }];We are required to write a JavaScript function that takes in one such array and extracts a separate array for each object property.Therefore, one array for the name property of each object, one for total and one for value. If there existed more properties, we would have separated more ... Read More

Checking if a key exists in a JavaScript object

AmitDiwan
Updated on 20-Nov-2020 10:30:11

335 Views

We are required to illustrate the correct way to check whether a particular key exists in an object or not. Before moving on to the correct way let's first examine an incorrect way and see how actually its incorrect.Way 1: Checking for undefined value (incorrect way)Due to the volatile nature of JavaScript, we might want to check for the existence of key in an object like this −const obj = { name: 'Rahul' };if(!obj['fName']){}orif(obj['fName'] === undefined){}These both are incorrect ways. Why?Because in this case there happens to be no 'fName' key, but suppose there existed a 'fName' which was deliberately ... Read More

Check if an array is growing by the same margin in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:28:03

74 Views

We are required to write a JavaScript function that takes in an array of numbers. Our function should return true if the difference between all adjacent elements is the same positive number, false otherwise.ExampleThe code for this will be −const arr = [4, 7, 10, 13, 16, 19, 22]; const growingMarginally = arr => {    if(arr.length

Use array as sort order in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:26:40

91 Views

const sort = ["this", "is", "my", "custom", "order"]; const myObjects = [    {"id":1, "content":"is"},    {"id":2, "content":"my"},    {"id":3, "content":"this"},    {"id":4, "content":"custom"},    {"id":5, "content":"order"} ];We are required to write a JavaScript function that takes in two such arrays and sorts the second array of objects on the basis of the first array so that the content property of objects are matched with the strings of the first array.Therefore, for the above arrays the output should look like −const output = [    {"id":3, "content":"this"},    {"id":1, "content":"is"},    {"id":2, "content":"my"},    {"id":4, "content":"custom"},    {"id":5, "content":"order"} ];ExampleThe ... Read More

Return indexes of greatest values in an array in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:24:27

300 Views

We are required to write a JavaScript function that takes in an array of Numbers. The array may contain more than one greatest element (i.e., repeating greatest element).We are required to write a JavaScript function that takes in one such array and returns all the indices of the greatest element.ExampleThe code for this will be −const arr = [10, 5, 4, 10, 5, 10, 6]; const findGreatestIndices = arr => {    const val = Math.max(...arr);    const greatest = arr.reduce((indexes, element, index) => {       if(element === val){          return indexes.concat([index]);       } else {          return indexes;       };    }, []);    return greatest; } console.log(findGreatestIndices(arr));OutputAnd the output in the console will be −[ 0, 3, 5 ]

Advertisements