Repeating only even numbers inside an array in JavaScript

AmitDiwan
Published on 22-Oct-2020 16:56:48
We are required to write a JavaScript function that should repeat the even number inside the same array.For example, given the following array −const arr = [1, 2, 5, 6, 8];OutputWe should get the output −const output = [1, 2, 2, 5, 6, 6, 8, 8];Therefore, let’s write the code ... Read More

Checking for ascending arrays in JavaScript

AmitDiwan
Published on 22-Oct-2020 16:54:23
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.Note: sequence a0, a1, ..., an is considered to be strictly increasing if a0 < a1 < ... < an. Sequences containing ... Read More

Snail Trail Problem in JavaScript

AmitDiwan
Published on 22-Oct-2020 16:52:54
Suppose we have an array like this −const arr = [    [1, 2, 3, 4],    [12, 13, 14, 5],    [11, 16, 15, 6],    [10, 9, 8, 7] ];The array is bound to be a square matrix.We are required to write a JavaScript function that takes in ... Read More

Best way to find length of JSON object in JavaScript

AmitDiwan
Published on 22-Oct-2020 16:51:10
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 ... Read More

Removing property from a JSON object in JavaScript

AmitDiwan
Published on 22-Oct-2020 16:47:25
Let’s say, we create 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 as follows −const myObject = {    "ircEvent": "PRIVMSG",   ... Read More

Using one array to help filter the other in JavaScript

AmitDiwan
Published on 22-Oct-2020 16:43:58
Suppose, we have an array and objects like these −Objects: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}, ];Array:const names ... Read More

Converting two arrays into a JSON object in JavaScript

AmitDiwan
Published on 22-Oct-2020 16:40:11
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 = [   ... Read More

Replacing zero starting with whitespace in JavaScript

AmitDiwan
Published on 22-Oct-2020 16:38:48
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. 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"ExampleThe code ... Read More

Square root function without using Math.sqrt() in JavaScript

AmitDiwan
Published on 22-Oct-2020 16:36:54
We are required to write a JavaScript function that takes in a number and calculates its square root without using the Math.sqrt() function.Therefore, let’s write the code for this function −ExampleThe code for this will be −const square = (n, i, j) => {    let mid = (i + ... Read More

Mapping string to Numerals in JavaScript

AmitDiwan
Published on 22-Oct-2020 16:34:49
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 = 25Note: Remove ... Read More
1 2 3 4 5 6 7 ... 4145 Next
Advertisements