Found 10483 Articles for Web Development

Capitalize letter in a string in order and create an array to store - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:22:49

120 Views

We are required to write a JavaScript function that takes in a string and turns it into a Mexican Wave i.e. resembling string produced by successive captial letters in every word −For example −If the string is −const str = 'edabit';Then the output should be the following i.e. successive single capital letter −const output = ["Edabit", "eDabit", "edAbit", "edaBit", "edabIt", "edabiT"];ExampleFollowing is the code −const str = 'edabit'; const replaceAt = function(index, char){    let a = this.split("");    a[index] = char;    return a.join(""); }; String.prototype.replaceAt = replaceAt; const createEdibet = word => {    let array = word.split('') ... Read More

Sorting an array of objects by property values - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:21:20

389 Views

Suppose, we have an array of objects like this −const homes = [    {        "h_id": "3",        "city": "Dallas",        "state": "TX",        "zip": "75201",        "price": "162500"    }, {        "h_id": "4",        "city": "Bevery Hills",        "state": "CA",        "zip": "90210",        "price": "319250"    }, {        "h_id": "5",        "city": "New York",        "state": "NY",        "zip": "00010",        "price": "962500"    } ... Read More

Finding the length of a JavaScript object

AmitDiwan
Updated on 01-Oct-2020 10:19:54

216 Views

Suppose we have an object like this −const obj = {    name: "Ramesh",    age: 34,    occupation: "HR Manager",    address: "Tilak Nagar, New Delhi",    experience: 13 };We are required to write a JavaScript function on Objects that computes their size (i.e., the number of properties in it).ExampleFollowing is the code −const obj = {    name: "Ramesh",    age: 34,    occupation: "HR Manager",    address: "Tilak Nagar, New Delhi",    experience: 13 }; Object.prototype.size = function(obj) {    let size = 0, key;    for (key in obj) {       if (obj.hasOwnProperty(key)){          size++       };    };    return size; }; const size = Object.size(obj); console.log(size);This will produce the following output on console −5

How do we remove a property from a JavaScript object? - JavaScript

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

300 Views

Let’s say, we have an object as follows −const myObject = {    "ircEvent": "PRIVMSG",    "method": "newURI",    "regex": "^http://.*" };We are required to illustrate the best way to remove the property regex to end up with new myObject?Following is the solution −const myObject = {    "ircEvent": "PRIVMSG",    "method": "newURI" };The delete operator is used to remove properties from objects.const myObject = {    "ircEvent": "PRIVMSG",    "method": "newURI",    "regex": "^http://.*" }; delete myObject['regex']; console.log(myObject.hasOwnProperty("regex")); // falseThe delete operator in JavaScript has a different function to that of the keyword in C and C++ −It ... Read More

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

263 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

343 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

528 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

702 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

172 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

Advertisements