Suppose, we have an array of objects like this −const arr = [ { "parentIndex": '0' , "childIndex": '3' , "parent": "ROOT", "child": "root3" }, { "parentIndex": '3' , "childIndex": '2' , "parent": "root3" , "child": "root2" }, { "parentIndex": '3' , "childIndex": '1' , "parent": "root3" , "child": "root1" } ];We are required to write a JavaScript ... Read More
Suppose, we have three arrays of numbers like these −const code = [123, 456, 789]; const year = [2013, 2014, 2015]; const period = [3, 4, 5];We are required to write a JavaScript function that takes in three such arrays. The function should then construct an array of objects based on these three arrays like this −const output = [ {"code": 123, "year": 2013, "period": 3}, {"code": 456, "year": 2014, "period": 4}, {"code": 789, "year": 2015, "period": 5} ];ExampleThe code for this will be −const code = [123, 456, 789]; const year = [2013, 2014, 2015]; const ... Read More
We are required to write a JavaScript function that takes in an array of strings as the first argument and a single character as the second argument.The function should return true if the character specified by second argument exists in any of the strings of the array, false otherwise.ExampleThe code for this will be −const arr = ['first', 'second', 'third', 'last']; const searchForLetter = (arr = [], letter = '') => { for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(!el.includes(letter)){ continue; ... Read More
Suppose, we have an array of objects like this −const arr = [ { assigned_user:{ name:'Paul', id: 34158 }, doc_status: "processed" }, { assigned_user:{ name:'Simon', id: 48569 }, doc_status: "processed" }, { assigned_user:{ name:'Simon', id: 48569 }, doc_status: "processed" } ];We are required ... Read More
We are required to write a JavaScript program that provides the user an input to enter a string value.The program should then check the input value against some hard-coded array values. Our program should print true to the screen if the input string value is included in the array, false otherwise.ExampleThe code for this will be − CHECK EXISTENCE const arr = ['arsenal', 'chelsea', 'everton', 'fulham', 'swansea']; const checkExistence = () => { const userInput = document.getElementById("input").value; const exists = arr.includes(userInput); document.getElementById('result').innerText = exists; }; Check OutputAnd the output on the screen will be −
Suppose, we have an array of strings that contains some duplicate entries like this −const arr = ['blue', 'blue', 'green', 'blue', 'yellow', 'yellow', 'green'];We are required to write a JavaScript function that takes in one such array. The function should merge all the duplicate entries with one another.Therefore, the output for the above input should look like this −const output = ['blueblue', 'green', 'blue', 'yellowyellow', 'green'];ExampleThe code for this will be −const arr = ['blue', 'blue', 'green', 'blue', 'yellow', 'yellow', 'green']; const combineDuplicate = (arr = []) => { let prev = null; const groups = arr.reduce((acc, value) ... Read More
Suppose, we have an array of objects like this −const arr = [{ name: 'Paul', country: 'Canada', }, { name: 'Lea', country: 'Italy', }, { name: 'John', country: 'Italy', }, ];We are required to devise a way to filter an array of objects depending on a string keyword. The search has to be made in any properties of the object.For instance −When we type "lea", we want to go through all the objects and all their properties to return the objects that contain "lea". When we type "italy", we want to go through all ... Read More
Suppose, we have an array of objects like this −const arr = [ {id:123, value:"value1", name:"Name1"}, {id:124, value:"value2", name:"Name1"}, {id:125, value:"value3", name:"Name2"}, {id:126, value:"value4", name:"Name2"} ];Note that some of the "name" property in objects within the array are duplicate.We are required to write a JavaScript function that takes in one such array of objects. The function should then construct a new array of strings that contains only unique "name" property value from the array.Therefore, the output for the above input should look like this −const output = ["Name1", "Name2"];ExampleThe code for this will be −const arr ... Read More
Suppose, we have an array of objects containing data about some cars like this −const arr = [ { 'make': 'audi', 'model': 'r8', 'year': '2012' }, { 'make': 'audi', 'model': 'rs5', 'year': '2013' }, { 'make': 'ford', 'model': 'mustang', 'year': '2012' }, { 'make': 'ford', 'model': 'fusion', 'year': '2015' }, { 'make': 'kia', ... Read More
We are required to write a JavaScript function that takes in three argument, namely:day, month and year. Based on these three inputs, our function should find the day of the week on that date.For example: If the inputs are −day = 15, month = 8, year = 1993OutputThen the output should be −const output = 'Sunday'ExampleThe code for this will be −const dayOfTheWeek = (day, month, year) => { // JS months start at 0 return dayOfTheWeekJS(day, month - 1, year); } function dayOfTheWeekJS(day, month, year) { const DAYS = [ 'Sunday', ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP