Found 9297 Articles for Object Oriented Programming

How to exclude certain values from randomly generated array JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:38:09

1K+ Views

We have to create a function that takes in 2 arguments: an integer and an array of integers. First argument denotes the length of array we have to return and the second argument contains the elements that should not be present in our return array. Actually, we need an array of random numbers between 0 to 100 but it should not include any element that’s present in the argument array.Note − No two numbers should be duplicate.Let’s call our function generateRandom(). The code for this would be −Exampleconst absentArray = [44, 65, 5, 34, 87, 42, 8, 76, 21, 33]; ... Read More

JavaScript Converting array of objects into object of arrays

AmitDiwan
Updated on 19-Aug-2020 06:35:48

614 Views

Let’s say, we have the following data about a team arranged in an array of objects, our task is to convert this data into an object with key as role and value as array of player names.Our sample array −const team = [{    role: 'Batsman',    player: 'V Kohli' }, {    role: 'Wicket Keeper',    player: 'KL Rahul' }, {    role: 'Batsman',    player: 'R Sharma' }, {    role: 'Wicket Keeper',    player: 'R Pant' }, {    role: 'Bowler',    player: 'B Kumar' }, {    role: 'Bowler',    player: 'M Shami' }, ]Let’s define ... Read More

JavaScript array.includes inside nested array returning false where as searched name is in array

AmitDiwan
Updated on 18-Aug-2020 07:43:39

1K+ Views

It is a well-known dilemma that when we use includes() inside nested arrays i.e., multidimensional array, it does not work, there exists a Array.prototype.flat() function which flats the array and then searches through but it’s browser support is not very good yet.So our job is to create a includesMultiDimension() function that takes in an array and a string and returns a boolean based on the presence/absence of that string in the array.There exist many solutions to this problem, most of them includes recursion, heavy array function, loops and what not.What we are going to discuss here is by far the ... Read More

When summing values from 2 arrays how can I cap the value in the new JavaScript array?

AmitDiwan
Updated on 18-Aug-2020 07:42:25

70 Views

Suppose, we have two arrays both containing three elements each, the corresponding values of red, green, blue color in integer.Our job is to add the corresponding value to form an array for new rgb color and also making sure if any value adds up to more than 255, we that value to 255.Therefore, let’s define a function addColors() that takes in two arguments, both arrays and returns a new array based on the input.The code for this will be −Exampleconst color1 = [45, 125, 216]; const color2 = [89, 180, 78]; const addColors = (color1, color2) => {    const newColor = color1.map((val, index) => {       return val + color2[index]

Title Case A Sentence JavaScript

AmitDiwan
Updated on 18-Aug-2020 07:41:22

232 Views

Let’s say we are required to write a function that accepts a string and capitalizes the first letter of every word in that string and changes the case of all the remaining letters to lowercase.For example, if the input string is −hello world coding is very interestingThe output should be −Hello World Coding Is Very InterestingLet’s define a function capitaliseTitle() that takes in a string and capitalises the first letter of each word and returns the string −Examplelet str = 'hello world coding is very interesting'; const capitaliseTitle = (str) => {    const string = str    .toLowerCase()   ... Read More

How to find the one integer that appears an odd number of times in a JavaScript array?

AmitDiwan
Updated on 18-Aug-2020 07:40:26

112 Views

We are given an array of integers and told that all the elements appear for an even number of times except a single element. Our job is to find that element in single iteration.Let this be the sample array −[1, 4, 3, 4, 2, 3, 2, 7, 8, 8, 9, 7, 9]Before attempting this problem, we need to understand a little about the bitwise XOR (^) operator.The XOR operator returns TRUE if both the operands are complementary to each other and returns FALSE if both the operands are the same.TRUTH TABLE OF XOR () operator −0 ^ 0 → 0 ... Read More

Adding two arrays of objects with existing and repeated members of two JavaScript arrays replacing the repeated ones

AmitDiwan
Updated on 18-Aug-2020 07:39:37

103 Views

We have the following arrays of objects, we need to merge them into one removing the objects that have redundant value for the property name −const first = [{    name: 'Rahul',    age: 23 }, {    name: 'Ramesh',    age: 27 }, {    name: 'Vikram',    age: 35 }, {    name: 'Harsh',    age: 34 }, {    name: 'Vijay',    age: 21 }]; const second = [{    name: 'Vijay',    age: 21 }, {    name: 'Vikky',    age: 20 }, {    name: 'Joy',    age: 26 }, {    name: 'Vijay',   ... Read More

Merge object properties through unique field then print data - JavaScript

AmitDiwan
Updated on 18-Aug-2020 07:36:56

91 Views

Let’s say we have a students object containing two properties names and marks. The names is an array of object with each object having two properties name and roll, similarly marks is an array of object with each object having properties mark and roll. Our task is to combine the marks and names properties according to the appropriate roll property of each object.The students object is given here −const students = {    marks: [{       roll: 123,       mark: 89    }, {       roll: 143,       mark: 69    }, ... Read More

How to print star pattern in JavaScript in a very simple manner?

AmitDiwan
Updated on 18-Aug-2020 07:32:48

1K+ Views

Here is a simple star pattern that we are required to print inside the JavaScript console. Note that it has to be printed inside the console and not in the output or HTML window −* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Here’s the code for doing so in JavaScript −Exampleconst star = "* "; //where length is no of stars in longest streak const length = 6; for(let i = 1; i

JavaScript : Why does % operator work on strings? - (Type Coercion)

AmitDiwan
Updated on 18-Aug-2020 07:29:27

241 Views

Let’s say we have a code snippet here that produces some startling results. Firstly, we see that the modulo operator is working fine with strings as well (surprisingly). Secondly, concatenation of two strings produces awkward results.We need to explain why JavaScript does so?Here’s the problem code −Exampleconst numStr = '127'; const result = numStr % 5; const firstName = 'Armaan'; const lastName = 'Malik'; const fullName = firstName + + lastName; console.log('modulo result: ', result); console.log('full name: ', fullName);Outputmodulo result: 2 full name: ArmaanNaNBefore jumping into the code, let’s first learn a bit about one of the most fundamental topics ... Read More

Advertisements