Let’s say the following is our array −var listOfNames = [ 'John', 'David', 'Bob' ];Following is our button −Click The Button To get the Reverse ValueNow, for array items in reverse, at first, reach the array length and decrement the length by 1. Then, print the contents of the particular index in reverse order.Example Live Demo Document Click The Button To get the Reverse Value var listOfNames = [ 'John', 'David', 'Bob' ]; count=listOfNames.length-1; function reverseTheArray(){ document.getElementById('reverseTheArray').innerHTML = listOfNames[count]; count--; if (count
In this tutorial, we will be discussing a program to find maximum sum of pairs with specific difference.For this we will be provided with an array containing integers and a value K. Our task is to pair elements having difference less than K and finally find the maximum sum of the elements in disjoint sets.Example Live Demo#include using namespace std; //returning maximum sum of disjoint pairs int maxSumPairWithDifferenceLessThanK(int arr[], int N, int K){ sort(arr, arr+N); int dp[N]; dp[0] = 0; for (int i = 1; i < N; i++) { dp[i] = dp[i-1]; ... Read More
To assign multiple variables to the same value, the syntax is as followsvar anyVariableName1, anyVariableName2, anyVariableName3……….N; yourVariableName1=yourVariableName2=yourVariableName3=.........N=yourValue;Let’s say the following are our variables and we are assigning same value −var first, second, third, fourth, fifth; first=second=third=fourth=fifth=100;Examplevar first, second, third, fourth, fifth; first=second=third=fourth=fifth=100; console.log(first); console.log(second); console.log(third); console.log(fourth); console.log(fifth); console.log("The sum of all values="+(first+second+third+fourth+fifth));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo114.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo114.js 100 100 100 100 100 The sum of all values=500Read More
In this tutorial, we will be discussing a program to find maximum sum of nodes in Binary tree such that no two are adjacent.For this we will be provided with a binary tree. Our task is to find the subset having maximum sum such that no two nodes in subset are directly connected.Example Live Demo#include using namespace std; //binary tree node structure struct node { int data; struct node *left, *right; }; struct node* newNode(int data) { struct node *temp = new struct node; temp->data = data; temp->left = temp->right = NULL; return temp; ... Read More
To convert a JavaScript object to key value, you need to use Object.entries() along with map(). Following is the code −Examplevar studentObject={ 101: "John", 102: "David", 103: "Bob" } var studentDetails = Object.assign({}, studentObject) studentDetails = Object.entries(studentObject).map(([studentId,studentName])=>({studentId ,studentName})); console.log(studentDetails);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo113.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo113.js [ { studentId: '101', studentName: 'John' }, { studentId: '102', studentName: 'David' }, { studentId: '103', studentName: 'Bob' } ]
For this, use map() along with join(‘’).Then ‘’ is for new line. Following is the code −ExamplestudentDetails = [ [101, 'John', 'JavaScript'], [102, 'Bob', 'MySQL'] ]; var studentFormat = '||Id||Name||subjName||'; var seperate = ''; seperate = seperate + studentDetails.map(obj => `|${obj.join('|')}|`).join(''); studentFormat = studentFormat + seperate; console.log(studentFormat);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo112.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo112.js ||Id||Name||subjName|| |101|John|JavaScript| |102|Bob|MySQL|
In this tutorial, we will be discussing a program to find maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming.For this we will be provided with a binary tree. Our task is to find the subset having maximum sum such that no two nodes in the subset are directly connected using Dynamic Programming.Example Live Demo#include using namespace std; //finding diameter using dynamic programming void dfs(int node, int parent, int dp1[], int dp2[], list* adj, int tree[]){ int sum1 = 0, sum2 = 0; for (auto i = adj[node].begin(); i != adj[node].end(); ... Read More
Let’s say the following is our current array −var listOfNames=["John", "Mike", "Sam", "Carol"];Use JSON.parse(JSON.stringify()) to create new array and set values from the old array above.Examplefunction createNewArray(listOfNames) { return JSON.parse(JSON.stringify(listOfNames)); } var listOfNames=["John", "Mike", "Sam", "Carol"]; var namesArray = listOfNames.slice(); console.log("The new Array="); console.log(namesArray);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo111.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo111.js The new Array= [ 'John', 'Mike', 'Sam', 'Carol' ]Read More
In this tutorial, we will be discussing a program to find maximum sum of increasing order elements from n arrays.For this we will be provided with N arrays of M size. Our task is to find the maximum sum by selecting one element from each array such that element from the previous array is smaller than the next one.Example Live Demo#include #define M 4 using namespace std; //calculating maximum sum by selecting //one element int maximumSum(int a[][M], int n) { for (int i = 0; i < n; i++) sort(a[i], a[i] + M); int sum ... Read More
Let’s say, we are matching the current date and time with the business hours. We need to display whether the office is closed or open right now on the basis of current time.Get the hours from the current date and can use the ternary operator for close and open. Following is the code −Example Live Demo Document const gettingHours = new Date().getHours() const actualHours = (gettingHours >= 10 && gettingHours < 18) ? 'Open' : 'Closed'; document.querySelector('.closeOrOpened').innerHTML = actualHours; To run the above program, save ... Read More