Separating Digits of a Number in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:59:13

1K+ Views

We are required to write a JavaScript program that provides user with a input. When the user inputs some value and press the button, our function should check if the input is a valid number, if it is a valid number, the program should print all digits of the number separately to the screen.For example − If the input is −43354Then the output on the screen should be −4 3 3 5 4Let us write the code for this function −The code for this will be −HTML                     ... Read More

Convert Number to Alphabets in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:59:09

836 Views

We are required to write a JavaScript function that takes in a string of any variable length that represents a number.Our function is supposed to convert the number string to the corresponding letter string.For example − If the number string is −const str = '78956';Then the output should be −const output = 'ghief';If the number string is −const str = '12345';Then the output string should be −const output = 'lcde';Notice how we didn't convert 1 and 2 to alphabets separately because 12 also represents an alphabet. So we have to consider this case while writing our function.We, here, assume that ... Read More

Sort Object of Objects in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:57:55

2K+ Views

Suppose we have an Object of Objects like this −const obj = {    "CAB": {       name: 'CBSSP',       position: 2    },    "NSG": {       name: 'NNSSP',       position: 3    },    "EQU": {       name: 'SSP',       position: 1    } };We are required to write a JavaScript function that takes in one such array and sorts the sub-objects on the basis of the 'position' property of sub-objects (either in increasing or decreasing order).ExampleThe code for this will be −const obj = { ... Read More

Search by ID and Remove Object from JSON Array in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:55:56

3K+ Views

Suppose, we have an array of objects that contains data about some movies like this −const arr = [    {id: "1", name: "Snatch", type: "crime"},    {id: "2", name: "Witches of Eastwick", type: "comedy"},    {id: "3", name: "X-Men", type: "action"},    {id: "4", name: "Ordinary People", type: "drama"},    {id: "5", name: "Billy Elliot", type: "drama"},    {id: "6", name: "Toy Story", type: "children"} ];We are required to write a JavaScript function that takes in one such array as the first argument and an id string as the second argument. Then our function should search for the object ... Read More

List All Duplicate Values in a Nested JavaScript Object

AmitDiwan
Updated on 21-Nov-2020 09:54:16

1K+ Views

Suppose, we have a nested object that contains data about some pets like this −const pets = {    owner1: 'Frank',    owner2: 'Curly',    owner3: 'Maurice',    dogs: {       terriers: {          name1: 'Fido',          name2: 'Woofy',          name3: {             goodDog: 'Frank',             badDog: 'Judas',          }       },       poodles: {          name1: 'Curly',          name2: 'Fido',       },   ... Read More

Find Keys for Matched Values Like SQL Query in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:52:33

399 Views

Suppose, we have an object like this −const obj = {"100":"Jaipur", "101":"Delhi", "102":"Raipur", "104":"Goa"};We are required to write a JavaScript function that takes in one such object as the first argument and a search query term as the second argument. Then our function should return all those key/value pairs whose value includes the search term provided to the function as the second argument.We will simply iterate through the object, building the resulting object (if it matches the condition) as we move through and lastly return that object.ExampleThe code for this will be −const obj = {    "100":"Jaipur",    "101":"Delhi", ... Read More

Combine Highest Key Values of Multiple Arrays into a Single Array in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:51:25

235 Views

We are required to write a JavaScript function that takes in any number of array of Numbers. Our function should return an array of greatest numbers picked from the input array of array. The number of elements in the output array should be equal to the number of subarrays contained in the original input array.ExampleThe code for this will be −const arr1 = [117, 121, 18, 24]; const arr2 = [132, 19, 432, 23]; const arr3 = [32, 23, 137, 145]; const arr4 = [900, 332, 23, 19]; const mergeGreatest = (...arrs) => {    const res = [];   ... Read More

Sorting Parts of Array Separately in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:50:22

209 Views

We have an array that contains many objects. We are required to write a function to sort the first half of the array in ascending order.And the second half of the array with ascending order to but without inter mixing the entries of halves into one another.Consider this sample array −const arr = [    {id:1, x: 33},    {id:2, x: 22},    {id:3, x: 11},    {id:4, x: 3},    {id:5, x: 2},    {id:6, x: 1} ];Our function should sort this array on the basis of the 'x' property of the objects keeping the things mentioned above in ... Read More

Split Object into an Array of Objects in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:49:07

3K+ Views

Suppose, we have an object like this −const obj = {    "value 0": "value",    "value 1": "value",    "value 2": "value",    "value 3": "value",    "value 4": "value",    "value 5": "value",    "value 6": "value",    "value 7": "value",    "value 8": "value",    "value 9": "value" };We are required to write a JavaScript function that takes in one such object. The function should return a new array of objects in which each key/value pair is separated into its own separate object.ExampleThe code for this will be −const obj = {    "value 0": "value",   ... Read More

Get Total Number of Same Objects in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:47:49

145 Views

Suppose, we have an array of objects describing the routes of some flights like this −const routes = [    {       flyFrom: "CDG",       flyTo: "DUB",       return: 0,    },    {       flyFrom: "DUB",       flyTo: "SXF",       return: 0,    },    {       flyFrom: "SFX",       flyTo: "CDG",       return: 1,    } ];We need to count how many times there is return − 0 and how many times there is the return: 1.The end output should ... Read More

Advertisements