Found 9321 Articles for Object Oriented Programming

Count by unique key in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:40:57

529 Views

Suppose, we have an array of objects like this −const arr = [    {       assigned_user:{          name:'Paul',          id: 34158       },       doc_status: "processed"    },    {       assigned_user:{          name:'Simon',          id: 48569       },       doc_status: "processed"    },    {       assigned_user:{          name:'Simon',          id: 48569       },       doc_status: "processed"    } ];We are required ... Read More

Check if user inputted string is in the array in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:38:41

402 Views

We are required to write a JavaScript program that provides the user an input to enter a string value.The program should then check the input value against some hard-coded array values. Our program should print true to the screen if the input string value is included in the array, false otherwise.ExampleThe code for this will be −            CHECK EXISTENCE           const arr = ['arsenal', 'chelsea', 'everton', 'fulham',       'swansea'];       const checkExistence = () => {          const userInput = document.getElementById("input").value;          const exists = arr.includes(userInput);          document.getElementById('result').innerText = exists;       };            Check     OutputAnd the output on the screen will be −

Reduce an array to groups in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:37:04

162 Views

Suppose, we have an array of strings that contains some duplicate entries like this −const arr = ['blue', 'blue', 'green', 'blue', 'yellow', 'yellow', 'green'];We are required to write a JavaScript function that takes in one such array. The function should merge all the duplicate entries with one another.Therefore, the output for the above input should look like this −const output = ['blueblue', 'green', 'blue', 'yellowyellow', 'green'];ExampleThe code for this will be −const arr = ['blue', 'blue', 'green', 'blue', 'yellow', 'yellow', 'green']; const combineDuplicate = (arr = []) => {    let prev = null;    const groups = arr.reduce((acc, value) ... Read More

Filter array of objects whose properties contains a value in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:35:51

814 Views

Suppose, we have an array of objects like this −const arr = [{    name: 'Paul',    country: 'Canada', }, {    name: 'Lea',    country: 'Italy', }, {    name: 'John',    country: 'Italy', }, ];We are required to devise a way to filter an array of objects depending on a string keyword. The search has to be made in any properties of the object.For instance −When we type "lea", we want to go through all the objects and all their properties to return the objects that contain "lea". When we type "italy", we want to go through all ... Read More

Remove duplicates and map an array in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:32:37

501 Views

Suppose, we have an array of objects like this −const arr = [    {id:123, value:"value1", name:"Name1"},    {id:124, value:"value2", name:"Name1"},    {id:125, value:"value3", name:"Name2"},    {id:126, value:"value4", name:"Name2"} ];Note that some of the "name" property in objects within the array are duplicate.We are required to write a JavaScript function that takes in one such array of objects. The function should then construct a new array of strings that contains only unique "name" property value from the array.Therefore, the output for the above input should look like this −const output = ["Name1", "Name2"];ExampleThe code for this will be −const arr ... Read More

How to group an array of objects by key in JavaScript

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

889 Views

Suppose, we have an array of objects containing data about some cars like this −const arr = [    {       'make': 'audi',       'model': 'r8',       'year': '2012'    }, {       'make': 'audi',       'model': 'rs5',       'year': '2013'    }, {       'make': 'ford',       'model': 'mustang',       'year': '2012'    }, {       'make': 'ford',       'model': 'fusion',       'year': '2015'    }, {       'make': 'kia',       ... Read More

Finding day of week from date (day, month, year) in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:28:11

561 Views

We are required to write a JavaScript function that takes in three argument, namely:day, month and year. Based on these three inputs, our function should find the day of the week on that date.For example: If the inputs are −day = 15, month = 8, year = 1993OutputThen the output should be −const output = 'Sunday'ExampleThe code for this will be −const dayOfTheWeek = (day, month, year) => {    // JS months start at 0    return dayOfTheWeekJS(day, month - 1, year); } function dayOfTheWeekJS(day, month, year) {    const DAYS = [       'Sunday',     ... Read More

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

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

440 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

371 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

537 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

Advertisements