Found 9326 Articles for Object Oriented Programming

Finding the power of a string from a string with repeated letters in JavaScript

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

434 Views

The power of the string is the maximum length of a non−empty substring that contains only one unique character.We are required to write a JavaScript function that takes in a string and returns its power.For example −const str = "abbcccddddeeeeedcba"Then the output should be 5, because the substring "eeeee" is of length 5 with the character 'e' only.ExampleThe code for this will be −const str = "abbcccddddeeeeedcba" const maxPower = (str = '') => {    let power = 1    const sz = str.length - 1    for(let i = 0; i < sz; ++i) {       ... Read More

Checking for straight lines in JavaScript

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

368 Views

We are required to write a JavaScript function that takes in an array of arrays. Each subarray will contain exactly two items, representing the x and y coordinates respectively.Our function should check whether or not the coordinates specified by these subarrays form a straight line.For example −[[4, 5], [5, 6]] should return true.The array is guaranteed to contain at least two subarrays.ExampleThe code for this will be −const coordinates = [    [4, 5],    [5, 6] ]; const checkStraightLine = (coordinates = []) => {    if(coordinates.length === 0) return false;    let x1 = coordinates[0][0];    let y1 = coordinates[0][1];    let slope1 = null;    for(let i=1;i

Converting numbers to base-7 representation in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:23:03

522 Views

Like the base−2 representation (binary), where we repeatedly divide the base 10 (decimal) numbers by 2, in the base 7 system we will repeatedly divide the number by 7 to find the binary representation.We are required to write a JavaScript function that takes in any number and finds its base 7 representation.For example −base7(100) = 202ExampleThe code for this will be −const num = 100; const base7 = (num = 0) => {    let sign = num < 0 && '−' || '';    num = num * (sign + 1);    let result = '';    while (num) {       result = num % 7 + result;       num = num / 7 ^ 0;    };    return sign + result || "0"; }; console.log(base7(num));OutputAnd the output in the console will be −202

Add number strings without using conversion library methods in JavaScript

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

105 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

177 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

627 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

682 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

329 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

Advertisements