Found 9313 Articles for Object Oriented Programming

Reverse array with for loops JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:27:05

268 Views

We have to write a function that takes in an array and returns its reverse. Find its reverse using the for loop.Our sample array −const arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6];So, let’s write the code for this function −Exampleconst arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6]; const reverse =(arr) => {    const duplicate = arr.slice();    const reversedArray = [];    const { length } = arr;    for(let i = 0; i < length; i++){       reversedArray.push(duplicate.pop());    };    return reversedArray; }; console.log(reverse(arr));OutputThe output in the console will be −[    6, 43, -12, 12, 8,    7, 5, 4, 3, 2,    7 ]

Make array numbers negative JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:23:33

734 Views

Let’s say, the following is our array −const arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6];We are required to write a function that takes in the above array and returns an array with all the corresponding elements of array change to their negative counterpart (like 4 to -4, 6 to -6).If the element is already negative, then we should leave the element unchanged. Let’s write the code for this function −Exampleconst arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6]; const changeToNegative = (arr) => {    return arr.reduce((acc, val) => ... Read More

Adding only odd or even numbers JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:21:27

769 Views

We are required to make a function that given an array of numbers and a string that can take any of the two values “odd” or “even”, adds the numbers which match that condition. If no values match the condition, 0 should be returned.For example −console.log(conditionalSum([1, 2, 3, 4, 5], "even")); => 6 console.log(conditionalSum([1, 2, 3, 4, 5], "odd")); => 9 console.log(conditionalSum([13, 88, 12, 44, 99], "even")); => 144 console.log(conditionalSum([], "odd")); => 0So, let’s write the code for this function, we will use the Array.prototype.reduce() method here −Exampleconst conditionalSum = (arr, condition) => {    const add = (num1, num2) ... Read More

Append the current array with the squares of corresponding elements of the array in JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:19:32

75 Views

We have an array of Numbers like this −const arr = [12, 19, 5, 7, 9, 11, 21, 4];We have to write a function that takes in such an array and returns a new array with all the items of the original array appended by the squares of corresponding elements of the array.For this sample array, the output should be −[12, 19, 5, 7, 9, 11, 21, 4, 144, 361, 25, 49, 81, 121, 441, 16]Exampleconst arr = [12, 19, 5, 7, 9, 11, 21, 4]; const multiplyArray = (arr) => {    return arr.reduce((acc, val) => {       return acc.concat(val * val);    }, arr); }; console.log(multiplyArray(arr));OutputThe output in the console will be −[    12, 19, 5, 7, 9, 11,    21, 4, 144, 361, 25, 49,    81, 121, 441, 16 ]

Convert JavaScript array iteration result into a single line text string

AmitDiwan
Updated on 21-Aug-2020 14:16:44

209 Views

Let’s say, we have a string and an array −const textString = 'Convert javascript array iteration result into a single line text string. Happy searching!'; const keywords = ['integer', 'javascript', 'dry', 'Happy', 'exam'];We have to write a function that maps the array to a string containing only true and false depending on the fact whether the corresponding array element is present in the string or not.Exampleconst textString = 'Convert javascript array iteration result into a single line text string. Happy searching!'; const keywords = ['integer', 'javascript', 'dry', 'Happy', 'exam']; const includesString = (arr, str) => {    return arr.reduce((acc, val) ... Read More

Inverse operation in JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:13:52

784 Views

Let’s say, we have to write a function that takes in a binary string (consisting of only 0 and 1) and returns its inverse, all 0s replaced by 1 and 1s replaced by 0.Let’s write the code for this function −Exampleconst num = '1101'; const n = '11010111'; const inverseBinary = (binary) => {    return binary.split("").map(el => {       return `${1- parseInt(el, 10)}`    }).join(""); }; console.log(inverseBinary(num)); console.log(inverseBinary(n));OutputThe output in the console will be −0010 00101000

Merge objects in array with similar key JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:10:14

1K+ Views

Let’s say, we have the following array of objects −const arr = [    {id: 1, h1: 'Daily tests'},    {id: 2, h1: 'Details'},    {id: 1, h2: 'Daily classes'},    {id: 3, h2: 'Results'},    {id: 2, h3: 'Admissions'},    {id: 1, h4: 'Students'},    {id: 2, h5: 'Alumni'},    {id: 3, h3: 'Appreciations'},    {id: 1, h5: 'Tiny Tots'},    {id: 1, h6: 'Extras'}, ];We have to write a function that converts this array into an array where all the headings (h1, h2, h3 …) that have the same id get clubbed inside the same object. Therefore, let’s ... Read More

Order an array of words based on another array of words JavaScript

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

273 Views

Let’s say, we have the following array of objects sorted according to its id property −const unordered = [{    id: 1,    string: 'sometimes' }, {    id: 2,    string: 'be' }, {    id: 3,    string: 'can' }, {    id: 4,    string: 'life' }, {    id: 5,    string: 'tough' }, {    id: 6,    string: 'very' }, ];And another array of string like this −const ordered = ['Life', 'sometimes', 'can', 'be', 'very', 'tough'];We have to sort the first array so its string property have the same order of string as it ... Read More

How to Sort object of objects by its key value JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:55:27

528 Views

Let’s say, we have an object with keys as string literals and their values as objects as well like this −const companies = {    'landwaves ltd': {employees: 1200, worth: '1.2m', CEO: 'Rajiv Bansal'},    'colin & co': {employees: 200, worth: '0.2m', CEO: 'Sukesh Maheshwari'},    'motilal biscuits': {employees: 975, worth: '1m', CEO: 'Rahul Gupta'},    'numbtree': {employees: 1500, worth: '1.5m', CEO: 'Jay Kumar'},    'solace pvt ltd': {employees: 1800, worth: '1.65m', CEO: 'Arvind Sangal'},    'ambicure': {employees: 170, worth: '0.1m', CEO: 'Preetam Chawla'},    'dis n dat': {employees: 540, worth: '1m', CEO: 'Mohit Sharma'}, }We are required to write ... Read More

How to combine 2 arrays into 1 object in JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:51:33

645 Views

Let’s say, we have two arrays of equal lengths and are required to write a function that maps the two arrays into an object. The corresponding elements of the first array becomes the corresponding keys of the object and the elements of the second array become the value.We will reduce the first array, at the same time accessing elements of the second array by index. The code for this will be −Exampleconst keys = [    'firstName',    'lastName',    'isEmployed',    'occupation',    'address',    'salary',    'expenditure' ]; const values = [    'Hitesh',    'Kumar',    false, ... Read More

Advertisements