AmitDiwan has Published 10744 Articles

Index of closest element in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Oct-2020 11:51:40

199 Views

Suppose we have an array like this −const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362];We are required to write a JavaScript function that takes in one such array and a number, say n.The function should return the index of item from the array which is ... Read More

Repeating only even numbers inside an array in JavaScript

AmitDiwan

AmitDiwan

Updated on 22-Oct-2020 13:27:19

189 Views

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

AmitDiwan

Updated on 22-Oct-2020 13:25:08

174 Views

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

AmitDiwan

Updated on 22-Oct-2020 13:23:23

337 Views

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

Using one array to help filter the other in JavaScript

AmitDiwan

AmitDiwan

Updated on 22-Oct-2020 13:16:14

100 Views

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

Replacing zero starting with whitespace in JavaScript

AmitDiwan

AmitDiwan

Updated on 22-Oct-2020 13:09:22

406 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. 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

AmitDiwan

Updated on 22-Oct-2020 13:08:00

4K+ 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.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

AmitDiwan

Updated on 22-Oct-2020 13:05:36

786 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 = 25Note: Remove ... Read More

Finding the smallest fitting number in JavaScript

AmitDiwan

AmitDiwan

Updated on 22-Oct-2020 13:01:47

143 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a number which can exactly divide all the numbers in the array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [4, 6, 34, 76, 78, ... Read More

Sorting Array without using sort() in JavaScript

AmitDiwan

AmitDiwan

Updated on 22-Oct-2020 12:59:41

7K+ Views

We are required to write a JavaScript function that takes in an array of numbers.The function should sort the array using the Array.prototype.sort() method, but, here, we are required to use the Array.prototype.reduce() method to sort the array.Therefore, let’s write the code for this function −ExampleThe code for this will ... Read More

Advertisements