We are required to write a JavaScript function that takes in an array of literals as the first argument and a string as the second argument. Our function should return the count of the number of times that string (provided by second argument) appears anywhere in the array.ExampleThe code for this will be −const arr = ["word", "a word", "another word"]; const query = "word"; const findAll = (arr, query) => { let count = 0; count = arr.filter(el => { return el.indexOf(query) != -1; }).length; return count; }; console.log(findAll(arr, query));OutputAnd the output in the console will be −3
Suppose, we have a JSON Array that contains data about some tickets like this −const arr = [ { "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": "1", "description": "Regular Ticket to Event" }, { "quantity": "1", "description": "Regular Ticket to ... Read More
We are required to write a JavaScript function that takes in two arrays of equal length. The function should multiply the corresponding (by index) values in each, and sum the results.For example: If the input arrays are −const arr1 = [2, 3, 4, 5]; const arr2 = [4, 3, 3, 1];then the output should be 34, because,(4*2+3*3+4*3+5*1) = 34ExampleThe code for this will be −const arr1 = [2, 3, 4, 5]; const arr2 = [4, 3, 3, 1]; const produceAndAdd = (arr1 = [], arr2 = []) => { let sum = 0; for(let i=0; i < arr1.length; i++) { const product = (arr1[i] * arr2[i]); sum += product; }; return sum; }; console.log(produceAndAdd(arr1, arr2));OutputAnd the output in the console will be −34
We are required to write a JavaScript function that splits an Array of numbers into N groups, which must be ordered from larger to smaller groups.For example, in the below code, split an Array of 12 numbers into 5 Arrays, and the result should be evenly split, from large (group) to small:const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; const output = [[1, 2, 3] [4, 5, 6] [7, 8] [9, 10] [11, 12]];The function should take in the array as the first argument and the number of partitions as the second argument.ExampleThe ... Read More
We are required to write a JavaScript function that takes in a stringified mathematical equation. The function should return the result of the equation provided to the function.For example: If the equation is −const str = '1+23+4+5-30';Then the output should be 3ExampleThe code for this will be −const str = '1+23+4+5-30'; const compute = (str = '') => { let total = 0; str = str.match(/[+\−]*(\.\d+|\d+(\.\d+)?)/g) || []; while (str.length) { total += parseFloat(str.shift()); }; return total; }; console.log(compute(str));OutputAnd the output in the console will be −3
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
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
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP