Found 9318 Articles for Object Oriented Programming

Validating email and password - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:16:38

2K+ Views

Suppose, we have this dummy array that contains the login info of two of many users of a social networking platform −const array = [{    email: 'usman@gmail.com',    password: '123'  },  {    email: 'ali@gmail.com',    password: '123'  } ];We are required to write a JavaScript function that takes in an email string and a password string.The function should return a boolean based on the fact whether or not the user exists in the database.ExampleFollowing is the code −const array = [{    email: 'usman@gmail.com',    password: '123' }, {    email: 'ali@gmail.com',    password: '123' }]; const matchCredentials ... Read More

The Zombie Apocalypse case study - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:15:03

167 Views

A nasty zombie virus is spreading out in the digital cities. We work at the digital CDC and our job is to look over the city maps and tell which areas are contaminated by the zombie virus so the digital army would know where to drop the bombs.They are the new kind of digital zombies which can travel only in vertical and horizontal directions and infect only numbers same as them.We'll be given a two-dimensional array with numbers in it.For some mysterious reason patient zero is always found in north west area of the city (element [0][0] of the matrix) ... Read More

Filter one array with another array - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:13:02

277 Views

Suppose, we have an array and objects like the following −const main = [    {name: "Karan", age: 34},    {name: "Aayush", age: 24},    {name: "Ameesh", age: 23},    {name: "Joy", age: 33},    {name: "Siddarth", age: 43},    {name: "Nakul", age: 31},    {name: "Anmol", age: 21}, ]; const names = ["Karan", "Joy", "Siddarth", "Ameesh"];We are required to write a JavaScript function that takes in two such arrays and filters the first array in place to contain only those objects whose name property is included in the second array.ExampleFollowing is the code −const main = [ {name: "Karan", ... Read More

How to insert an element into all positions in an array using recursion - JavaScript?

AmitDiwan
Updated on 01-Oct-2020 10:11:02

418 Views

We are required to declare a function, let’s say insertAllPositions, which takes two arguments −an element, x, and an array, arr. Functions must return an array of arrays, with each array corresponding to arr with x inserted in a possible position.That is, if arr is the length N, then the result is an array with N + 1 arrays −For example, the result of insertAllPositions(10, [1, 2, 3]) should be −const output = [    [10, 1, 2, 3],    [1, 10, 2, 3],    [1, 2, 10, 3],    [1, 2, 3, 10] ];We are required to write this ... Read More

How to return object from an array with highest key values along with name - JavaScript?

AmitDiwan
Updated on 01-Oct-2020 10:09:18

596 Views

Suppose, we have an array of objects that contains information about marks of some students in a test −const students = [    { name: 'Andy', total: 40 },    { name: 'Seric', total: 50 },    { name: 'Stephen', total: 85 },    { name: 'David', total: 30 },    { name: 'Phil', total: 40 },    { name: 'Eric', total: 82 },    { name: 'Cameron', total: 30 },    { name: 'Geoff', total: 30 } ];We are required to write a JavaScript function that takes in one such array and returns a object with the name and ... Read More

How can we make an Array of Objects from n properties of n arrays in JavaScript?

AmitDiwan
Updated on 01-Oct-2020 10:07:00

126 Views

Suppose we have two arrays of literals like these −const options = ['A', 'B', 'C', 'D']; const values = [true, false, false, false];We are required to write a JavaScript function that creates and returns a new Array of Objects from these two arrays, like this −const response = [    {opt: 'A', val: true},    {opt: 'B', val: false},    {opt: 'C', val: false},    {opt: 'D', val: false}, ];ExampleFollowing is the code −const options = ['A', 'B', 'C', 'D']; const values = [true, false, false, false]; const mapArrays = (options, values) => {    const res = [];   ... Read More

Return 5 random numbers in range, first number cannot be zero - JavaScript

AmitDiwan
Updated on 30-Sep-2020 14:37:35

114 Views

We are required to write a JavaScript function that generates an array of exactly five unique random numbers. The condition is that all the numbers have to be in the range [0, 9] and the first number cannot be 0.ExampleFollowing is the code −const fiveRandoms = () => {    const arr = []    while (arr.length < 5) {       const random = Math.floor(Math.random() * 10);       if (arr.indexOf(random) > -1){          continue;       };       if(!arr.length && !random){          continue;       }       arr[arr.length] = random;    }    return arr; }; console.log(fiveRandoms());OutputThis will produce the following output in console −[ 9, 0, 8, 5, 4 ]

How to replace leading zero with spaces - JavaScript

AmitDiwan
Updated on 30-Sep-2020 14:36:30

210 Views

We are required to write a JavaScript function that takes in a string that represents a number. Replace the leading zero with spaces in the number. We need to make sure the prior spaces in number are retained.For example,If the string value is defined as −"   004590808"Then the output should come as −" 4590808"ExampleFollowing is the code −const str = ' 004590808'; const replaceWithSpace = str => {    let replaced = '';    const regex = new RegExp(/^\s*0+/);    replaced = str.replace(regex, el => {       const { length } = el;       return ' '.repeat(length);    });    return replaced; }; console.log(replaceWithSpace(str));OutputThis will produce the following output in console −4590808

Finding square root of a number without using library functions - JavaScript

AmitDiwan
Updated on 30-Sep-2020 14:35:30

445 Views

We are required to write a JavaScript function that takes in a number and calculates its square root without using the Math.sqrt() function.ExampleFollowing is the code −const square = (n, i, j) => {    let mid = (i + j) / 2;    let mul = mid * mid;    if ((mul === n) || (Math.abs(mul - n) < 0.00001)){       return mid;    }else if (mul < n){       return square(n, mid, j);    }else{       return square(n, i, mid);    } } // Function to find the square root of n ... Read More

Trying to get number for each character in string - JavaScript

AmitDiwan
Updated on 30-Sep-2020 14:34:18

169 Views

We are required to write a JavaScript function that takes in a string. It should print out each number for every corresponding letter in the string.For example, a = 1 b = 2 c = 3 d = 4 e = 5 . . . Y = 25 Z = 26Therefore, if the input is "hello man", Then the output should be a number for each character −"8, 5, 12, 12, 15, 13, 1, 14"ExampleFollowing is the code −const str = 'hello man'; const charPosition = str => {    str = str.split('');    const arr = [];    const ... Read More

Advertisements